libnavajo
web server for static and dynamic pages developement in C++
nvjThread.h
Go to the documentation of this file.
1 //********************************************************
12 //********************************************************
13 
14 #ifndef NVJTHREAD_H_
15 #define NVJTHREAD_H_
16 
17 #include <stdio.h>
18 #include <string.h>
19 
20 extern "C"
21 {
22  #include "pthread.h"
23 }
24 
25 #define STRERROR_BUF char strerror_buf[256]
26 
27 #ifdef WIN32
28 #define STRERROR(en) strerror_s(strerror_buf, sizeof strerror_buf, en)
29 
30 #else
31 
32 #define STRERROR(en) ( \
33  strerror_r(en, strerror_buf, sizeof strerror_buf) == 0 \
34  ? strerror_buf : "Unknown error" \
35 )
36 
37 #endif
38 
39 
40 
41 /***********************************************************************/
42 
43 inline void create_thread(pthread_t *thread_p,
44  void *(* thread_rtn)(void *), void *data_p)
45 {
46  pthread_attr_t thread_attr; /* Thread attributes */
47  int rc; /* Return code (error number) */
48  STRERROR_BUF; /* Buffer for strerror_r() */
49 
50 
51  rc = pthread_attr_init(&thread_attr);
52  if (rc != 0)
53  fprintf(stderr, "pthread_attr_init(): %s\n", STRERROR(rc));
54 
55  rc = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
56  if (rc != 0)
57  fprintf(stderr, "pthread_attr_setdetachstate(): %s\n", STRERROR(rc));
58 
59  rc = pthread_attr_setstacksize (&thread_attr, 512 * 1024); // par d�fault 2*1024*1024
60  if (rc != 0)
61  fprintf(stderr, "pthread_attr_setstacksize(): %s\n", STRERROR(rc));
62 
63  rc = pthread_create(thread_p, &thread_attr, thread_rtn, data_p);
64  if (rc != 0)
65  fprintf(stderr, "pthread_create(): %s\n", STRERROR(rc));
66 
67  rc = pthread_attr_destroy(&thread_attr);
68  if (rc != 0)
69  fprintf(stderr, "pthread_attr_destroy(): %s\n", STRERROR(rc));
70 
71  return;
72 }
73 
74 /***********************************************************************/
75 /*
76 * The cancel_thread() function requests the cancellation of the specified
77 * thread.
78 */
79 
80 inline void cancel_thread(pthread_t thread)
81 {
82  int rc; /* Return code (error number) */
83  STRERROR_BUF; /* Buffer for strerror_r() */
84 
85 
86  rc = pthread_cancel(thread);
87 
88  if (rc != 0)
89  fprintf(stderr, "pthread_cancel(): %s\n", STRERROR(rc));
90 
91  return;
92 }
93 
94 /***********************************************************************/
95 /*
96  * The wait_for_thread() function waits for the specified thread to
97  * terminate.
98  */
99 
100 inline void wait_for_thread(pthread_t thread)
101 {
102  int rc; /* Return code (error number) */
103  STRERROR_BUF; /* Buffer for strerror_r() */
104 
105  rc = pthread_join(thread, NULL);
106  if (rc != 0)
107  fprintf(stderr, "pthread_join(): %s\n", STRERROR(rc));
108 
109  return;
110 }
111 
112 /***********************************************************************/
113 
114 inline void cancelstate_thread(void)
115 {
116  int rc; /* Return code (error number) */
117  STRERROR_BUF; /* Buffer for strerror_r() */
118 
119  rc=pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
120  if (rc != 0)
121  fprintf(stderr, "pthread_setcancelstate(): %s\n", STRERROR(rc));
122 
123  rc=pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
124  if (rc != 0)
125  fprintf(stderr, "pthread_setcanceltype(): %s\n", STRERROR(rc));
126 }
127 
128 /***********************************************************************/
129 
130 struct cancelArg
131 {
132  pthread_t *p; // pthread to kill
133  unsigned s; // second
134  unsigned ns; // nanosecond
135 };
136 
137 #ifndef WIN32
138 inline void *thread_timeout_scheduler(void *arg)
139 {
140  cancelstate_thread();
141 
142  cancelArg *ca=(cancelArg*)arg;
143  pthread_t thread_to_kill=*(ca->p);
144 
145  timespec t1={ca->s, ca->ns};
146  timespec t2;
147 
148  if (nanosleep(&t1,&t2)==-1)
149  pthread_exit(0);
150 
151  if (t2.tv_nsec>0)
152  pthread_exit(0);
153 
154  cancel_thread(thread_to_kill);
155  wait_for_thread(thread_to_kill);
156  printf("Timeout Exceeded: The thread as been cancelled !!! \n");
157 
158  pthread_exit(0);
159  return NULL;
160 };
161 
162 #endif
163 
164 #endif
Definition: nvjThread.h:130