summaryrefslogtreecommitdiff
path: root/utility/rtos_compatibility_layers/posix/posix_signal_self_send_test.c
blob: 5847029cf8a9ea527d34286506f2acb68ca89a7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation                                */
/* Copyright (c) 2026 Eclipse ThreadX contributors                         */
/*                                                                         */
/* This program and the accompanying materials are made available under    */
/* the terms of the MIT License which is available at                      */
/* https://opensource.org/licenses/MIT.                                    */
/*                                                                         */
/* SPDX-License-Identifier: MIT                                            */
/***************************************************************************/

/* Simple self-signaling test.  */

#include   "pthread.h"

#define     DEMO_STACK_SIZE         2048
#if 0
#define     MAX_MESSAGE_SIZE        50
#endif
#define     DEMO_BYTE_POOL_SIZE     9120


/* Define the POSIX pthread object control blocks ... */

pthread_t               pthread_0;

#if 0
pthread_t               pthread_1;
pthread_t               pthread_2;
pthread_t               pthread_3;
pthread_t               pthread_4;
pthread_t               pthread_5;
#endif

/* Define pthread attributes objects */


pthread_attr_t          ptattr0;
#if 0
pthread_attr_t          ptattr1;
pthread_attr_t          ptattr2;
pthread_attr_t          ptattr3;
pthread_attr_t          ptattr4;
pthread_attr_t          ptattr5;


/* Define the message queue attribute.  */

struct mq_attr          queue_atrr;

/* Define a queue descriptor.          */

mqd_t                   q_des;

/* Define a semaphore.                 */

sem_t                   *sem;

/* Define a mutex                     */

pthread_mutex_t         mutex1;

/* Define a mutex attributes object   */

pthread_mutexattr_t     mta1;
#endif

/* Define the counters used in this test application...  */

ULONG     pthread_0_counter;
ULONG     pthread_0_signal_counter;


#if 0
ULONG     pthread_1_counter;
ULONG     pthread_2_counter;
ULONG     pthread_3_counter;
ULONG     pthread_4_counter;
ULONG     pthread_5_counter;
ULONG     pthread_1_message_sent;
ULONG     pthread_2_message_received;
#endif

/* Define pthread function prototypes.  */

VOID    *pthread_0_entry(VOID *);


/* Define signal handlers.  */

VOID    pthread_0_signal_handler(int);


#if 0
VOID    *pthread_1_entry(VOID *);
VOID    *pthread_2_entry(VOID *);
VOID    *pthread_3_entry(VOID *);
VOID    *pthread_4_entry(VOID *);
VOID    *pthread_5_entry(VOID *);


/* Message to be sent.  */
CHAR *msg0 = "This is a test message";
#endif
ULONG free_memory[192*1024 / sizeof(ULONG)];

/* Define main entry point.  */

INT main()
{

    /* Enter the ThreadX kernel.  */
    tx_kernel_enter();
}


/* Define what the initial system looks like.  */
VOID tx_application_define(VOID *first_unused_memory)
{

    VOID* storage_ptr;


struct sched_param  param;

#if 0
queue_atrr.mq_maxmsg  = 124;
queue_atrr.mq_msgsize = MAX_MESSAGE_SIZE;
#endif

    /* Init POSIX Wrapper */
    storage_ptr = (VOID*) posix_initialize((VOID*)free_memory);

    /* Put system definition stuff in here, e.g. pthread creates and other assoerted
       create information. */

    /* Create pthread attributes.  */
    pthread_attr_init(&ptattr0);
#if 0
    pthread_attr_init(&ptattr1);
    pthread_attr_init(&ptattr2);
    pthread_attr_init(&ptattr3);
    pthread_attr_init(&ptattr4);
    pthread_attr_init(&ptattr5);
#endif

    /* Create a sched_param structure */
    memset(&param, 0, sizeof(param));

    /* Now create all pthreads , firstly modify respective ptheread
       attribute with desired priority and stack start address and then create the pthread */

    /* Create pthread 0.  */
    param.sched_priority = 10;
    pthread_attr_setschedparam(&ptattr0, &param);
    pthread_attr_setstackaddr(&ptattr0,  storage_ptr );
    storage_ptr = (int *) storage_ptr + DEMO_STACK_SIZE;
    pthread_create (&pthread_0, &ptattr0,pthread_0_entry,NULL);

#if 0
    /* Create pthread 1.  */
    param.sched_priority = 15;
    pthread_attr_setschedparam(&ptattr1, &param);
    pthread_attr_setstackaddr(&ptattr1, (VOID*) storage_ptr );
    storage_ptr = storage_ptr + DEMO_STACK_SIZE;
    pthread_create (&pthread_1, &ptattr1,pthread_1_entry,NULL);

    /* Create pthread 2.  */
    param.sched_priority = 20;
    pthread_attr_setschedparam(&ptattr2, &param);
    pthread_attr_setstackaddr(&ptattr2, (VOID*) storage_ptr );
    storage_ptr = storage_ptr + DEMO_STACK_SIZE;
    pthread_create (&pthread_2, &ptattr2,pthread_2_entry,NULL);

    /* Create pthread 3.  */
    param.sched_priority = 25;
    pthread_attr_setschedparam(&ptattr3, &param);
    pthread_attr_setstackaddr(&ptattr3, (VOID*) storage_ptr );
    storage_ptr = storage_ptr + DEMO_STACK_SIZE;
    pthread_create (&pthread_3, &ptattr3,pthread_3_entry,NULL);

    /* Create pthread 4.  */
    param.sched_priority = 30;
    pthread_attr_setschedparam(&ptattr4, &param);
    pthread_attr_setstackaddr(&ptattr4, (VOID*) storage_ptr );
    storage_ptr = storage_ptr + DEMO_STACK_SIZE;
    pthread_create (&pthread_4, &ptattr4,pthread_4_entry,NULL);

    /* Create pthread 5.  */
    param.sched_priority = 5;
    pthread_attr_setschedparam(&ptattr5, &param);
    pthread_attr_setstackaddr(&ptattr5, (VOID*) storage_ptr );
    storage_ptr = storage_ptr + DEMO_STACK_SIZE;
    pthread_create (&pthread_5, &ptattr5,pthread_5_entry,NULL);

    /* Create a Message queue.  */
    q_des = mq_open("Queue",O_CREAT|O_RDWR,0,&queue_atrr);

    /* Create a Semaphore.  */
    sem = sem_open("Sem0", O_CREAT | O_EXCL,0,1);

    /* Create a Mutex */
    pthread_mutex_init(&mutex1, NULL);
#endif

}

VOID    error_handler(void)
{

    while(1)
    {
    }
}



/* Define the signal handler.  */

VOID  pthread_0_signal_handler(int signo)
{

    /* Check for pthread self call not pthread 0. The signal handler should appear to be
       called from pthread 0.  */
    if (pthread_self() != pthread_0)
    {

        /* Call error handler.  */
        error_handler();
    }

    /* Check for proper signal.  */
    if (signo != 15)
    {

        /* Call error handler.  */
        error_handler();
    }

    /* Just increment the signal counter for this test.  */
    pthread_0_signal_counter++;
}



#if 1
/* Define the test pthreads */
INT pt0_status=0;
#endif

/* Self signal test.  */

VOID    *pthread_0_entry(VOID *pthread0_input)
{

//    struct timespec thread_0_sleep_time={0,0};

    /* Register the signal handler.  */
    pt0_status =  signal(15, pthread_0_signal_handler);

    /* Check for error.  */
    if (pt0_status)
        error_handler();

    /* This pthread simply sits in while-forever-sleep loop */
    while(1)
    {
        /* Increment the pthread counter.*/
        pthread_0_counter++;

        /* Raise the signal.  */
        pt0_status =  pthread_kill(pthread_0, 15);

        /* Check for errors.  */
        if ((pt0_status) || (pthread_0_counter != pthread_0_signal_counter))
    {
            error_handler();
        break;
    }

#if 0
        /* sleep for a while  */
        thread_0_sleep_time.tv_nsec =  999999999;
         thread_0_sleep_time.tv_sec =  4;
    pt0_status=nanosleep(&thread_0_sleep_time,0);
    if(pt0_status)
        break;
#endif

    }

#if 1
    return(&pt0_status);
#endif
}

#if 0
INT    pt1_status=0;

VOID    *pthread_1_entry(VOID *pthread1_input)
{

    struct timespec thread_1_sleep_time={0,0};

    /* This thread simply sends a messages to a queue shared by pthread 2.  */
    while(1)
    {

        /* Increment the thread counter.  */
        pthread_1_counter++;
    /* Send message to queue 0.  */
        pt1_status = mq_send(q_des,msg0,strlen(msg0),3);

        /* check status.  */
        if(pt1_status)
           break;

        /* Increment the message sent.  */
        pthread_1_message_sent++;

     /* sleep for a while  */
        thread_1_sleep_time.tv_nsec = 200000000;
    nanosleep(&thread_1_sleep_time,0);
    }
    return(&pt1_status);
}


INT     pt2_status;

VOID    *pthread_2_entry(VOID *pthread2_input)
{

CHAR    msgr0[MAX_MESSAGE_SIZE];
ULONG   priority;
struct timespec thread_2_sleep_time={0,0};

    /* This pthread retrieves messages placed on the queue by pthread 1.  */
    while(1 )
    {
        /* Increment the thread counter.  */
        pthread_2_counter++;
        pt2_status = mq_receive(q_des,msgr0,MAX_MESSAGE_SIZE,&priority);

        if(pt2_status != 0)
           break;

        /* Otherwise, it is OK to increment the received message count.  */
        pthread_2_message_received++;
        /* sleep for a while  */
        thread_2_sleep_time.tv_nsec = 200000000;
    nanosleep(&thread_2_sleep_time,0);
    }
   return(&pt2_status);
}

INT    pt3_status;
VOID    *pthread_3_entry(VOID *pthread3_input)
{


struct timespec thread_3_sleep_time={0,0};


    /* This function compete for ownership of semaphore_0.  */
    while(1)
    {

        /* Increment the thread counter.  */
        pthread_3_counter++;
        /* Get the semaphore with suspension.  */
        pt3_status = sem_wait(sem);

        /* Check status.  */
        if (pt3_status)
          break;

        /* Sleep for a while to hold the semaphore.  */
        thread_3_sleep_time.tv_nsec = 200000000;
        nanosleep(&thread_3_sleep_time,0);

        /* Release the semaphore.  */
        pt3_status = sem_post(sem);

        /* Check status.  */
        if (pt3_status )
            break;
    }
    return(&pt3_status);
}

INT  pt4_status;

VOID    *pthread_4_entry(VOID *pthread4_input)
{

struct timespec thread_4_sleep_time={0,0};

    while(1)
    {

       /* Increment the thread counter.  */
       pthread_4_counter++;
       /* now lock the mutex */
       pt4_status = pthread_mutex_lock(&mutex1);
       if (pt4_status != OK)
       break;

       /* sleep for a while  */
       thread_4_sleep_time.tv_nsec = 200000000;
       nanosleep(&thread_4_sleep_time,0);

       pt4_status = pthread_mutex_unlock(&mutex1);
       if (pt4_status != OK)
       break;
    }
    return(&pt4_status);
}

INT     pt5_status;

VOID    *pthread_5_entry(VOID *pthread5_input)
{

struct timespec thread_5_sleep_time={0,0};

    while(1)
    {
        /* Increment the thread counter. */
        pthread_5_counter++;
        /* now lock the mutex */
        pt5_status = pthread_mutex_lock(&mutex1);
        if (pt5_status != OK)
            break;

        /* sleep for a while  */
        thread_5_sleep_time.tv_nsec = 20000000;
    nanosleep(&thread_5_sleep_time,0);
        pt5_status = pthread_mutex_unlock(&mutex1);
          if (pt5_status != OK)
          break;
    }
    return(&pt5_status);
}
#endif