]> rtime.felk.cvut.cz Git - can-benchmark.git/blob - rtems/gw/libs/load.c
Changed all (I think) files using CRLF to use LF.
[can-benchmark.git] / rtems / gw / libs / load.c
1 #include <stdio.h>
2
3 #include <pthread.h>
4 #include <semaphore.h>
5
6 #include "load.h"
7
8 /* Load function for threads. */
9 static void* produce(void* arg);
10 static void* consume(void* arg);
11
12 /* semaphores for consumer/producer pair */
13 static sem_t produced, consumed;
14 /* pthread handles for the loader */
15 static pthread_t consumer, producer;
16 static int n = 0;
17 static char running = 0;
18
19 static void* produce(void* arg){
20     while (1) {
21             sem_wait(&consumed);
22             n++; 
23             sem_post(&produced);
24             pthread_testcancel();
25     }
26     return NULL;
27 }       
28
29 static void* consume(void* arg){
30     while (1) {
31             sem_wait(&produced);
32             n--;
33             sem_post(&consumed);
34             pthread_testcancel();
35     }
36     return NULL;
37 }
38
39 /*
40 * This function starts threads loading the CPU and creates associated semaphores. 
41
42 * Has a guard to prevent starting again, before it was stopped.
43 *
44 * No error handling currently, only tries to report errors.
45 */
46 int start_thread_load(){
47     if (running == 0){
48         printf("Attempting to start load.\n");
49         int res;
50         running = 1;
51         res = sem_init(&consumed, 0, 0);
52         if (res < 0){
53             printf("Couldn't initialize consumed semaphore.\n");
54             return 1;
55         }
56         res = sem_init(&produced, 0, 1);
57         if (res < 0){
58             printf("Couldn't initialize produced semaphore.\n");
59             return 1;
60         }
61         
62         res = pthread_create(&producer, NULL, produce, NULL);
63         if (res < 0){
64             printf("Couldn't create producer thread.\n");
65             return 1;
66         }
67         
68         res = pthread_create(&consumer, NULL, consume, NULL);
69         if (res < 0){
70             printf("Couldn't create consumer thread.\n");
71             return 1;
72         }
73     
74         pthread_detach(producer);
75         pthread_detach(consumer);
76         printf("Load started succesfully.\n");
77         return 0;
78     } else {
79         printf("Load is already running.\n");
80         return 0;
81     }
82 }
83
84 /*
85 * This function stops threads loading the CPU and destroys associated semaphores. 
86 *
87 * Has a guard against attempting to stop the threads if they are not running.
88
89 * No error handling currently, only tries to report errors.
90 */
91 int end_thread_load(){
92     if (running == 1){
93         int res;
94         printf("Attempting to cancel producer thread.\n");
95         res = pthread_cancel(producer);
96         if (res != 0){
97             /* This means that sending cancel signal has failed... Just returning an error should be enough. */
98             /* If we killed the thread, destroying the semaphore would lead to UB. */
99             printf("Failed.\n");
100             return 1;
101         }
102
103         printf("Attempting to cancel consumer thread.\n");
104         res = pthread_cancel(consumer);
105         if (res != 0){
106             /* Same here. */
107             printf("Failed.\n");
108             return 1;
109         }
110
111         printf("Preparing to destroy semaphores.\n");
112         /* Wait a bit so that the threads can get to a cancellation point. */
113         sleep(1);
114         sem_destroy(&produced);
115         sem_destroy(&consumed);
116         running = 0;
117         printf("Finished.\n");
118         return 0;
119     } else {
120         printf("Load is not running.\n");
121         return 0;
122     }
123 }