home *** CD-ROM | disk | FTP | other *** search
/ PC Open 48 / pcopen48.iso / Internet / HtTrack / DATA1.CAB / Sources / src / htsthread.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-21  |  2.9 KB  |  101 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche, Yann Philippot
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. This project has been developed by Xavier Roche and Yann Philippot,
  29. from the company Serianet at Caen, France (http://www.serianet.com)
  30.  
  31. Please visit our Website: http://www.httrack.com
  32. */
  33.  
  34.  
  35. /* ------------------------------------------------------------ */
  36. /* File: Threads                                                */
  37. /* Author: Xavier Roche                                         */
  38. /* ------------------------------------------------------------ */
  39.  
  40.  
  41. #include "htsglobal.h"
  42. #include "htsthread.h"
  43.  
  44. // Threads - emulate _beginthread under Linux/Unix using pthread_XX
  45. // Some changes will have to be done, see PTHREAD_RETURN,PTHREAD_TYPE
  46. #if USE_PTHREAD
  47. #include <pthread.h>    /* _beginthread, _endthread */
  48.  
  49. unsigned long _beginthread( void* ( *start_address )( void * ), unsigned stack_size, void *arglist )
  50. {
  51.   pthread_t th;
  52.   int retcode;
  53.   /* create a thread */
  54.   retcode = pthread_create(&th, NULL, start_address, arglist);
  55.   if (retcode != 0)     /* error */
  56.     return -1;
  57.   /* detach the thread from the main process so that is can be independent */
  58.   pthread_detach(th);
  59.   return 0;
  60. }
  61. #endif
  62.  
  63. #if USE_BEGINTHREAD
  64. /* 
  65.   Simple lock function
  66.  
  67.   Return value: always 0
  68.   Parameter:
  69.   1 wait for lock (mutex) available and lock it
  70.   0 unlock the mutex
  71.   [-1 check if locked (always return 0 with mutex)]
  72.   -999 initialize
  73. */
  74. int htsSetLock(PTHREAD_LOCK_TYPE* hMutex,int lock) {
  75. #if HTS_WIN
  76.   /* lock */
  77.   if (lock==1)
  78.     WaitForSingleObject(*hMutex,INFINITE);
  79.   /* unlock */
  80.   else if (lock==0)
  81.     ReleaseMutex(*hMutex);
  82.   /* create */
  83.   else if (lock==-999)
  84.     *hMutex=CreateMutex(NULL,FALSE,NULL);
  85. #else
  86.   /* lock */
  87.   if (lock==1)
  88.     pthread_mutex_lock(hMutex);
  89.   /* unlock */
  90.   else if (lock==0)
  91.     pthread_mutex_unlock(hMutex);
  92.   /* create */
  93.   else if (lock==-999)
  94.     pthread_mutex_init(hMutex,0);
  95. #endif
  96.   return 0;
  97. }
  98.  
  99. #endif
  100.  
  101.