17ec681f3Smrg/**************************************************************************
27ec681f3Smrg *
37ec681f3Smrg * Copyright 2020 Lag Free Games, LLC
47ec681f3Smrg * All Rights Reserved.
57ec681f3Smrg *
67ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
77ec681f3Smrg * copy of this software and associated documentation files (the
87ec681f3Smrg * "Software"), to deal in the Software without restriction, including
97ec681f3Smrg * without limitation the rights to use, copy, modify, merge, publish,
107ec681f3Smrg * distribute, sub license, and/or sell copies of the Software, and to
117ec681f3Smrg * permit persons to whom the Software is furnished to do so, subject to
127ec681f3Smrg * the following conditions:
137ec681f3Smrg *
147ec681f3Smrg * The above copyright notice and this permission notice (including the
157ec681f3Smrg * next paragraph) shall be included in all copies or substantial portions
167ec681f3Smrg * of the Software.
177ec681f3Smrg *
187ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
197ec681f3Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
207ec681f3Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
217ec681f3Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
227ec681f3Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
237ec681f3Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
247ec681f3Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
257ec681f3Smrg *
267ec681f3Smrg **************************************************************************/
277ec681f3Smrg
287ec681f3Smrg#ifndef RWLOCK_H
297ec681f3Smrg#define RWLOCK_H
307ec681f3Smrg
317ec681f3Smrg#ifdef _WIN32
327ec681f3Smrg#include <windows.h>
337ec681f3Smrg#else
347ec681f3Smrg#include <pthread.h>
357ec681f3Smrg#endif
367ec681f3Smrg
377ec681f3Smrg#ifdef __cplusplus
387ec681f3Smrgextern "C" {
397ec681f3Smrg#endif
407ec681f3Smrg
417ec681f3Smrgstruct u_rwlock
427ec681f3Smrg{
437ec681f3Smrg#ifdef _WIN32
447ec681f3Smrg   SRWLOCK rwlock;
457ec681f3Smrg#else
467ec681f3Smrg   pthread_rwlock_t rwlock;
477ec681f3Smrg#endif
487ec681f3Smrg};
497ec681f3Smrg
507ec681f3Smrgstatic inline int u_rwlock_init(struct u_rwlock *rwlock)
517ec681f3Smrg{
527ec681f3Smrg#ifdef _WIN32
537ec681f3Smrg   InitializeSRWLock(&rwlock->rwlock);
547ec681f3Smrg   return 0;
557ec681f3Smrg#else
567ec681f3Smrg   return pthread_rwlock_init(&rwlock->rwlock, NULL);
577ec681f3Smrg#endif
587ec681f3Smrg}
597ec681f3Smrg
607ec681f3Smrgstatic inline int u_rwlock_destroy(struct u_rwlock *rwlock)
617ec681f3Smrg{
627ec681f3Smrg#ifdef _WIN32
637ec681f3Smrg   return 0;
647ec681f3Smrg#else
657ec681f3Smrg   return pthread_rwlock_destroy(&rwlock->rwlock);
667ec681f3Smrg#endif
677ec681f3Smrg}
687ec681f3Smrg
697ec681f3Smrgstatic inline int u_rwlock_rdlock(struct u_rwlock *rwlock)
707ec681f3Smrg{
717ec681f3Smrg#ifdef _WIN32
727ec681f3Smrg   AcquireSRWLockShared(&rwlock->rwlock);
737ec681f3Smrg   return 0;
747ec681f3Smrg#else
757ec681f3Smrg   return pthread_rwlock_rdlock(&rwlock->rwlock);
767ec681f3Smrg#endif
777ec681f3Smrg}
787ec681f3Smrg
797ec681f3Smrgstatic inline int u_rwlock_rdunlock(struct u_rwlock *rwlock)
807ec681f3Smrg{
817ec681f3Smrg#ifdef _WIN32
827ec681f3Smrg   ReleaseSRWLockShared(&rwlock->rwlock);
837ec681f3Smrg   return 0;
847ec681f3Smrg#else
857ec681f3Smrg   return pthread_rwlock_unlock(&rwlock->rwlock);
867ec681f3Smrg#endif
877ec681f3Smrg}
887ec681f3Smrg
897ec681f3Smrgstatic inline int u_rwlock_wrlock(struct u_rwlock *rwlock)
907ec681f3Smrg{
917ec681f3Smrg#ifdef _WIN32
927ec681f3Smrg   AcquireSRWLockExclusive(&rwlock->rwlock);
937ec681f3Smrg   return 0;
947ec681f3Smrg#else
957ec681f3Smrg   return pthread_rwlock_wrlock(&rwlock->rwlock);
967ec681f3Smrg#endif
977ec681f3Smrg}
987ec681f3Smrg
997ec681f3Smrgstatic inline int u_rwlock_wrunlock(struct u_rwlock *rwlock)
1007ec681f3Smrg{
1017ec681f3Smrg#ifdef _WIN32
1027ec681f3Smrg   ReleaseSRWLockExclusive(&rwlock->rwlock);
1037ec681f3Smrg   return 0;
1047ec681f3Smrg#else
1057ec681f3Smrg   return pthread_rwlock_unlock(&rwlock->rwlock);
1067ec681f3Smrg#endif
1077ec681f3Smrg}
1087ec681f3Smrg
1097ec681f3Smrg#ifdef __cplusplus
1107ec681f3Smrg}
1117ec681f3Smrg#endif
1127ec681f3Smrg
1137ec681f3Smrg#endif
114