"Fossies" - the Fresh Open Source Software Archive

Member "apache-zookeeper-3.5.6/zookeeper-client/zookeeper-client-c/src/winport.h" (5 Oct 2019, 4432 Bytes) of package /linux/misc/apache-zookeeper-3.5.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "winport.h" see the Fossies "Dox" file reference documentation.

    1 /**
    2  * Licensed to the Apache Software Foundation (ASF) under one
    3  * or more contributor license agreements.  See the NOTICE file
    4  * distributed with this work for additional information
    5  * regarding copyright ownership.  The ASF licenses this file
    6  * to you under the Apache License, Version 2.0 (the
    7  * "License"); you may not use this file except in compliance
    8  * with the License.  You may obtain a copy of the License at
    9  *
   10  *     http://www.apache.org/licenses/LICENSE-2.0
   11  *
   12  * Unless required by applicable law or agreed to in writing, software
   13  * distributed under the License is distributed on an "AS IS" BASIS,
   14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15  * See the License for the specific language governing permissions and
   16  * limitations under the License.
   17  */
   18 
   19 /**
   20  * This header file is to port pthread lib , sockets and other utility methods on windows.
   21  * Specifically the threads function, mutexes, keys, and socket initialization.
   22  */
   23 
   24 #ifndef WINPORT_H_
   25 #define WINPORT_H_
   26 
   27 #ifdef WIN32
   28 #include "winconfig.h"
   29 
   30 #define _WINSOCK_DEPRECATED_NO_WARNINGS
   31 #include <winsock2.h> /* must always be included before ws2tcpip.h */
   32 #include <ws2tcpip.h> /* for struct sock_addr used in zookeeper.h */
   33 
   34 /* POSIX names are deprecated, use ISO conformant names instead. */
   35 #define strdup _strdup
   36 #define getcwd _getcwd
   37 #define getpid _getpid
   38 
   39 /* Windows "secure" versions of POSIX reentrant functions */
   40 #define strtok_r strtok_s
   41 #define localtime_r(a,b) localtime_s(b,a)
   42 
   43 /* After this version of MSVC, snprintf became a defined function,
   44    and so cannot be redefined, nor can #ifndef be used to guard it. */
   45 #if ((defined(_MSC_VER) && _MSC_VER < 1900) || !defined(_MSC_VER))
   46 #define snprintf _snprintf
   47 #endif
   48 
   49 
   50 #include <errno.h>
   51 #include <process.h>
   52 #include <stdint.h> /* for int64_t */
   53 #include <stdlib.h>
   54 #include <malloc.h>
   55 
   56 
   57 typedef int ssize_t;
   58 typedef HANDLE pthread_mutex_t;
   59 
   60 struct pthread_t_
   61 {
   62   HANDLE thread_handle;
   63   DWORD  thread_id;
   64 };
   65 
   66 typedef struct pthread_t_ pthread_t;
   67 typedef int pthread_mutexattr_t;       
   68 typedef int pthread_condattr_t;        
   69 typedef int pthread_attr_t; 
   70 #define PTHREAD_MUTEX_RECURSIVE 0
   71 
   72 int pthread_mutex_lock(pthread_mutex_t* _mutex );
   73 int pthread_mutex_unlock( pthread_mutex_t* _mutex );
   74 int pthread_mutex_init(pthread_mutex_t* _mutex, void* ignoredAttr);
   75 int pthread_mutex_destroy(pthread_mutex_t* _mutex);
   76 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, unsigned  (__stdcall* start_routine)(void* a), void *arg);
   77 int pthread_equal(pthread_t t1, pthread_t t2);
   78 pthread_t pthread_self();
   79 int pthread_join(pthread_t _thread, void** ignore);
   80 int pthread_detach(pthread_t _thread);
   81 
   82 void pthread_mutexattr_init(pthread_mutexattr_t* ignore);
   83 void pthread_mutexattr_settype(pthread_mutexattr_t* ingore_attr, int ignore);
   84 void pthread_mutexattr_destroy(pthread_mutexattr_t* ignore_attr);
   85 
   86 
   87 // http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
   88  
   89 typedef struct 
   90 {
   91        int waiters_count_;
   92     // Number of waiting threads.
   93 
   94     CRITICAL_SECTION waiters_count_lock_;
   95     // Serialize access to <waiters_count_>.
   96 
   97     HANDLE sema_;
   98        // Semaphore used to queue up threads waiting for the condition to
   99     // become signaled. 
  100 
  101     HANDLE waiters_done_;
  102     // An auto-reset event used by the broadcast/signal thread to wait
  103     // for all the waiting thread(s) to wake up and be released from the
  104     // semaphore. 
  105 
  106     size_t was_broadcast_;
  107     // Keeps track of whether we were broadcasting or signaling.  This
  108     // allows us to optimize the code if we're just signaling.
  109 }pthread_cond_t;
  110        
  111 int pthread_cond_init (pthread_cond_t *cv,const pthread_condattr_t * ignore);
  112 int pthread_cond_destroy(pthread_cond_t *cond);
  113 int pthread_cond_signal (pthread_cond_t *cv);
  114 int pthread_cond_broadcast (pthread_cond_t *cv);
  115 int pthread_cond_wait (pthread_cond_t *cv, pthread_mutex_t *external_mutex);
  116 
  117 
  118 struct pthread_key_t_
  119 {
  120   DWORD key;
  121   void (*destructor) (void *);  
  122 };
  123 
  124 typedef struct pthread_key_t_ pthread_key_t;
  125 int pthread_key_create(pthread_key_t *key, void (*destructor)(void *) );
  126 int pthread_key_delete(pthread_key_t key);
  127 void *pthread_getspecific(pthread_key_t key);
  128 int pthread_setspecific(pthread_key_t key, const void *value);
  129 
  130 int gettimeofday(struct timeval *tp, void *tzp);
  131 int close(SOCKET fd);
  132 int Win32WSAStartup();
  133 void Win32WSACleanup();
  134 double drand48(void);
  135 #endif //WIN32
  136 
  137 
  138 
  139 #endif //WINPORT_H_