home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win-source.exe / mysys / my_wincond.c < prev    next >
C/C++ Source or Header  |  2000-09-20  |  4KB  |  143 lines

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Library General Public
  5.    License as published by the Free Software Foundation; either
  6.    version 2 of the License, or (at your option) any later version.
  7.    
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU Library General Public
  14.    License along with this library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16.    MA 02111-1307, USA */
  17.  
  18. /*****************************************************************************
  19. ** The following is a simple implementation of posix conditions
  20. *****************************************************************************/
  21.  
  22. #undef SAFE_MUTEX            /* Avoid safe_mutex redefinitions */
  23. #include "mysys_priv.h"
  24. #if defined(THREAD) && defined(__WIN__)
  25. #include <m_string.h>
  26. #undef getpid
  27. #include <process.h>
  28. #include <sys/timeb.h>
  29.  
  30. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
  31. {
  32.   cond->waiting=0;
  33.   cond->semaphore=CreateSemaphore(NULL,0,0x7FFFFFFF,NullS);
  34.   if (!cond->semaphore)
  35.     return ENOMEM;
  36.   return 0;
  37. }
  38.  
  39. int pthread_cond_destroy(pthread_cond_t *cond)
  40. {
  41.     return CloseHandle(cond->semaphore) ? 0 : EINVAL;
  42. }
  43.  
  44.  
  45. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  46. {
  47.   InterlockedIncrement(&cond->waiting);
  48.   LeaveCriticalSection(mutex);
  49.   WaitForSingleObject(cond->semaphore,INFINITE);
  50.   InterlockedDecrement(&cond->waiting);
  51.   EnterCriticalSection(mutex);
  52.   return 0 ;
  53. }
  54.  
  55. int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  56.                            struct timespec *abstime)
  57. {
  58.   struct _timeb curtime;
  59.   int result;
  60.   long timeout;
  61.   _ftime(&curtime);
  62.   timeout= ((long) (abstime->tv_sec - curtime.time)*1000L +
  63.             (long)((abstime->tv_nsec/1000) - curtime.millitm)/1000L);
  64.   if (timeout < 0)                /* Some safety */
  65.     timeout = 0L;
  66.   InterlockedIncrement(&cond->waiting);
  67.   LeaveCriticalSection(mutex);
  68.   result=WaitForSingleObject(cond->semaphore,timeout);
  69.   InterlockedDecrement(&cond->waiting);
  70.   EnterCriticalSection(mutex);
  71.  
  72.   return result == WAIT_TIMEOUT ? ETIMEDOUT : 0;
  73. }
  74.  
  75.  
  76. int pthread_cond_signal(pthread_cond_t *cond)
  77. {
  78.   long prev_count;
  79.   if (cond->waiting)
  80.     ReleaseSemaphore(cond->semaphore,1,&prev_count);
  81.   return 0;
  82. }
  83.  
  84.  
  85. int pthread_cond_broadcast(pthread_cond_t *cond)
  86. {
  87.   long prev_count;
  88.   if (cond->waiting)
  89.     ReleaseSemaphore(cond->semaphore,cond->waiting,&prev_count);
  90.   return 0 ;
  91. }
  92.  
  93.  
  94. int pthread_attr_init(pthread_attr_t *connect_att)
  95. {
  96.   connect_att->dwStackSize    = 0;
  97.   connect_att->dwCreatingFlag    = 0;
  98.   connect_att->priority        = 0;
  99.   return 0;
  100. }
  101.  
  102. int pthread_attr_setstacksize(pthread_attr_t *connect_att,DWORD stack)
  103. {
  104.   connect_att->dwStackSize=stack;
  105.   return 0;
  106. }
  107.  
  108. int pthread_attr_setprio(pthread_attr_t *connect_att,int priority)
  109. {
  110.   connect_att->priority=priority;
  111.   return 0;
  112. }
  113.  
  114. int pthread_attr_destroy(pthread_attr_t *connect_att)
  115. {
  116.   bzero((gptr) connect_att,sizeof(*connect_att));
  117.   return 0;
  118. }
  119.  
  120. /****************************************************************************
  121. ** Fix localtime_r() to be a bit safer
  122. ****************************************************************************/
  123.  
  124. struct tm *localtime_r(const time_t *timep,struct tm *tmp)
  125. {
  126.   if (*timep == (time_t) -1)            /* This will crash win32 */
  127.   {
  128.     bzero(tmp,sizeof(*tmp));
  129.   }
  130.   else
  131.   {
  132.     struct tm *res=localtime(timep);
  133.     if (!res)                                   /* Wrong date */
  134.     {
  135.       bzero(tmp,sizeof(*tmp));                  /* Keep things safe */
  136.       return 0;
  137.     }
  138.     *tmp= *res;
  139.   }
  140.   return tmp;
  141. }
  142. #endif /* __WIN__ */
  143.