/[svn]/liblscp/trunk/src/thread.c
ViewVC logotype

Diff of /liblscp/trunk/src/thread.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 951 by capela, Sun Sep 24 12:55:48 2006 UTC revision 952 by capela, Tue Nov 28 22:46:32 2006 UTC
# Line 28  Line 28 
28    
29  struct _lscp_thread_t {  struct _lscp_thread_t {
30  #if defined(WIN32)  #if defined(WIN32)
31      HANDLE              hThread;          HANDLE              hThread;
32      DWORD               dwThreadID;          DWORD               dwThreadID;
33  #else  #else
34      pthread_t           pthread;          pthread_t           pthread;
35  #endif  #endif
36      lscp_thread_proc_t  pfnProc;          lscp_thread_proc_t  pfnProc;
37      void               *pvData;          void               *pvData;
38      int                 iDetach;          int                 iDetach;
39  };  };
40    
41    
# Line 45  static DWORD WINAPI _lscp_thread_start ( Line 45  static DWORD WINAPI _lscp_thread_start (
45  static void *_lscp_thread_start ( void *pvThread )  static void *_lscp_thread_start ( void *pvThread )
46  #endif  #endif
47  {  {
48      lscp_thread_t *pThread = (lscp_thread_t *) pvThread;          lscp_thread_t *pThread = (lscp_thread_t *) pvThread;
49      if (pThread) {          if (pThread) {
50      //  fprintf(stderr, "_lscp_thread_start: pThread=%p started.\n", pThread);          //  fprintf(stderr, "_lscp_thread_start: pThread=%p started.\n", pThread);
51          pThread->pfnProc(pThread->pvData);                  pThread->pfnProc(pThread->pvData);
52      //  fprintf(stderr, "_lscp_thread_start: pThread=%p terminated.\n", pThread);          //  fprintf(stderr, "_lscp_thread_start: pThread=%p terminated.\n", pThread);
53          if (pThread->iDetach)                  if (pThread->iDetach)
54              free(pThread);                          free(pThread);
55      }          }
56  #if defined(WIN32)  #if defined(WIN32)
57      return 0;          return 0;
58  #else  #else
59      return NULL;          return NULL;
60  #endif  #endif
61  }  }
62    
63  lscp_thread_t *lscp_thread_create ( lscp_thread_proc_t pfnProc, void *pvData, int iDetach )  lscp_thread_t *lscp_thread_create ( lscp_thread_proc_t pfnProc, void *pvData, int iDetach )
64  {  {
65      lscp_thread_t *pThread;          lscp_thread_t *pThread;
66  #if !defined(WIN32)  #if !defined(WIN32)
67      pthread_attr_t attr;          pthread_attr_t attr;
68  #endif  #endif
69    
70      if (pfnProc == NULL) {          if (pfnProc == NULL) {
71          fprintf(stderr, "lcsp_thread_create: Invalid thread function.\n");                  fprintf(stderr, "lcsp_thread_create: Invalid thread function.\n");
72          return NULL;                  return NULL;
73      }          }
74    
75      pThread = (lscp_thread_t *) malloc(sizeof(lscp_thread_t));          pThread = (lscp_thread_t *) malloc(sizeof(lscp_thread_t));
76      if (pThread == NULL) {          if (pThread == NULL) {
77          fprintf(stderr, "lcsp_thread_create: Out of memory.\n");                  fprintf(stderr, "lcsp_thread_create: Out of memory.\n");
78          return NULL;                  return NULL;
79      }          }
80      memset(pThread, 0, sizeof(lscp_thread_t));          memset(pThread, 0, sizeof(lscp_thread_t));
81    
82      pThread->pvData  = pvData;          pThread->pvData  = pvData;
83      pThread->pfnProc = pfnProc;          pThread->pfnProc = pfnProc;
84      pThread->iDetach = iDetach;          pThread->iDetach = iDetach;
85    
86  //  fprintf(stderr, "lscp_thread_create: pThread=%p.\n", pThread);  //  fprintf(stderr, "lscp_thread_create: pThread=%p.\n", pThread);
87    
88  #if defined(WIN32)  #if defined(WIN32)
89      pThread->hThread = CreateThread(NULL, 0, _lscp_thread_start, (LPVOID) pThread, 0, &(pThread->dwThreadID));          pThread->hThread = CreateThread(NULL, 0, _lscp_thread_start, (LPVOID) pThread, 0, &(pThread->dwThreadID));
90      if (pThread->hThread == NULL) {          if (pThread->hThread == NULL) {
91          fprintf(stderr, "lcsp_thread_create: Failed to create thread.\n");                  fprintf(stderr, "lcsp_thread_create: Failed to create thread.\n");
92          free(pThread);                  free(pThread);
93          return NULL;                  return NULL;
94      }          }
95  #else  #else
96      pthread_attr_init(&attr);          pthread_attr_init(&attr);
97      if (iDetach)          if (iDetach)
98          pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);                  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
99      if (pthread_create(&pThread->pthread, &attr, _lscp_thread_start, pThread)) {          if (pthread_create(&pThread->pthread, &attr, _lscp_thread_start, pThread)) {
100          fprintf(stderr, "lcsp_thread_create: Failed to create thread.\n");                  fprintf(stderr, "lcsp_thread_create: Failed to create thread.\n");
101          free(pThread);                  free(pThread);
102          return NULL;                  return NULL;
103      }          }
104  #endif  #endif
105    
106    return pThread;  return pThread;
107  }  }
108    
109    
110  lscp_status_t lscp_thread_join( lscp_thread_t *pThread )  lscp_status_t lscp_thread_join( lscp_thread_t *pThread )
111  {  {
112      lscp_status_t ret = LSCP_FAILED;          lscp_status_t ret = LSCP_FAILED;
113    
114      if (pThread == NULL)          if (pThread == NULL)
115          return ret;                  return ret;
116    
117  //  fprintf(stderr, "lscp_thread_join: pThread=%p.\n", pThread);  //  fprintf(stderr, "lscp_thread_join: pThread=%p.\n", pThread);
118    
119  #if defined(WIN32)  #if defined(WIN32)
120      if (pThread->hThread && WaitForSingleObject(pThread->hThread, INFINITE) == WAIT_OBJECT_0) {          if (pThread->hThread && WaitForSingleObject(pThread->hThread, INFINITE) == WAIT_OBJECT_0) {
121          pThread->hThread = NULL;                  pThread->hThread = NULL;
122          ret = LSCP_OK;                  ret = LSCP_OK;
123      }          }
124  #else  #else
125      if (pThread->pthread && pthread_join(pThread->pthread, NULL) == 0) {          if (pThread->pthread && pthread_join(pThread->pthread, NULL) == 0) {
126          pThread->pthread = 0;                  pThread->pthread = 0;
127          ret = LSCP_OK;                  ret = LSCP_OK;
128      }          }
129  #endif  #endif
130    
131      return ret;          return ret;
132  }  }
133    
134    
135  lscp_status_t lscp_thread_cancel ( lscp_thread_t *pThread )  lscp_status_t lscp_thread_cancel ( lscp_thread_t *pThread )
136  {  {
137      lscp_status_t ret = LSCP_FAILED;          lscp_status_t ret = LSCP_FAILED;
138    
139      if (pThread == NULL)          if (pThread == NULL)
140          return ret;                  return ret;
141    
142  //  fprintf(stderr, "lscp_thread_cancel: pThread=%p.\n", pThread);  //  fprintf(stderr, "lscp_thread_cancel: pThread=%p.\n", pThread);
143    
144  #if defined(WIN32)  #if defined(WIN32)
145      if (pThread->hThread) { // Should we TerminateThread(pThread->hThread, 0) ?          if (pThread->hThread) { // Should we TerminateThread(pThread->hThread, 0) ?
146      /*  pThread->hThread = NULL; */          /*  pThread->hThread = NULL; */
147          ret = LSCP_OK;                  ret = LSCP_OK;
148      }          }
149  #else  #else
150      if (pThread->pthread && pthread_cancel(pThread->pthread) == 0) {          if (pThread->pthread && pthread_cancel(pThread->pthread) == 0) {
151          pThread->pthread = 0;                  pThread->pthread = 0;
152          ret = LSCP_OK;                  ret = LSCP_OK;
153      }          }
154  #endif  #endif
155    
156      return ret;          return ret;
157  }  }
158    
159    
160  lscp_status_t lscp_thread_destroy ( lscp_thread_t *pThread )  lscp_status_t lscp_thread_destroy ( lscp_thread_t *pThread )
161  {  {
162      lscp_status_t ret = lscp_thread_cancel(pThread);          lscp_status_t ret = lscp_thread_cancel(pThread);
163    
164      if (ret == LSCP_OK)          if (ret == LSCP_OK)
165          ret = lscp_thread_join(pThread);                  ret = lscp_thread_join(pThread);
166                            
167  //  fprintf(stderr, "lscp_thread_destroy: pThread=%p.\n", pThread);  //  fprintf(stderr, "lscp_thread_destroy: pThread=%p.\n", pThread);
168    
169      if (ret == LSCP_OK)          if (ret == LSCP_OK)
170          free(pThread);                  free(pThread);
171    
172      return ret;          return ret;
173  }  }
174    
175    

Legend:
Removed from v.951  
changed lines
  Added in v.952

  ViewVC Help
Powered by ViewVC