my_wincond.c
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:4k
源码类别:

MySQL数据库

开发平台:

Visual C++

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