root/src/libsir/src/sirmutex.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. _sir_mutexcreate
  2. _sir_mutexlock
  3. _sir_mutextrylock
  4. _sir_mutexunlock
  5. _sir_mutexdestroy
  6. _sir_mutexcreate
  7. _sir_mutexlock
  8. _sir_mutextrylock
  9. _sir_mutexunlock
  10. _sir_mutexdestroy

   1 /*
   2  * sirmutex.c
   3  *
   4  * Version: 2.2.6
   5  *
   6  * -----------------------------------------------------------------------------
   7  *
   8  * SPDX-License-Identifier: MIT
   9  *
  10  * Copyright (c) 2018-2024 Ryan M. Lederman <lederman@gmail.com>
  11  *
  12  * Permission is hereby granted, free of charge, to any person obtaining a copy of
  13  * this software and associated documentation files (the "Software"), to deal in
  14  * the Software without restriction, including without limitation the rights to
  15  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  16  * the Software, and to permit persons to whom the Software is furnished to do so,
  17  * subject to the following conditions:
  18  *
  19  * The above copyright notice and this permission notice shall be included in all
  20  * copies or substantial portions of the Software.
  21  *
  22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  24  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  25  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  26  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28  *
  29  * -----------------------------------------------------------------------------
  30  */
  31 
  32 //-V::522
  33 #include "sir/mutex.h"
  34 #include "sir/internal.h"
  35 #include "sir/platform.h"
  36 
  37 #if !defined(__WIN__) /* pthread implementation */
  38 bool _sir_mutexcreate(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
  39     if (_sir_validptr(mutex)) {
  40         pthread_mutexattr_t attr;
  41         int op = pthread_mutexattr_init(&attr);
  42         if (0 == op) {
  43             op = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  44             if (0 == op) {
  45                 op = pthread_mutex_init(mutex, &attr);
  46                 if (0 == op)
  47                     return true;
  48             }
  49         }
  50 
  51         (void)_sir_handleerr(op);
  52     }
  53 
  54     return false;
  55 }
  56 
  57 bool _sir_mutexlock(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
  58     if (_sir_validptr(mutex)) {
  59         int op = pthread_mutex_lock(mutex);
  60         return 0 == op ? true : _sir_handleerr(op);
  61     }
  62 
  63     return false;
  64 }
  65 
  66 bool _sir_mutextrylock(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
  67     if (_sir_validptr(mutex)) {
  68         int op = pthread_mutex_trylock(mutex);
  69         return 0 == op ? true : _sir_handleerr(op);
  70     }
  71 
  72     return false;
  73 }
  74 
  75 bool _sir_mutexunlock(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
  76     if (_sir_validptr(mutex)) {
  77         int op = pthread_mutex_unlock(mutex);
  78         return 0 == op ? true : _sir_handleerr(op);
  79     }
  80 
  81     return false;
  82 }
  83 
  84 bool _sir_mutexdestroy(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
  85     if (_sir_validptr(mutex)) {
  86         int op = pthread_mutex_destroy(mutex);
  87         return 0 == op ? true : _sir_handleerr(op);
  88     }
  89 
  90     return false;
  91 }
  92 #else /* __WIN__ */
  93 bool _sir_mutexcreate(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
  94     if (_sir_validptr(mutex)) {
  95         InitializeCriticalSection(mutex);
  96         return true;
  97     }
  98 
  99     return false;
 100 }
 101 
 102 bool _sir_mutexlock(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
 103     if (_sir_validptr(mutex)) {
 104         EnterCriticalSection(mutex);
 105         return true;
 106     }
 107 
 108     return false;
 109 }
 110 
 111 bool _sir_mutextrylock(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
 112     if (_sir_validptr(mutex))
 113         return FALSE != TryEnterCriticalSection(mutex);
 114 
 115     return false;
 116 }
 117 
 118 bool _sir_mutexunlock(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
 119     if (_sir_validptr(mutex)) {
 120         LeaveCriticalSection(mutex);
 121         return true;
 122     }
 123 
 124     return false;
 125 }
 126 
 127 bool _sir_mutexdestroy(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
 128     if (_sir_validptr(mutex)) {
 129         DeleteCriticalSection(mutex);
 130         return true;
 131     }
 132 
 133     return false;
 134 }
 135 #endif /* !__WIN__ */

/* [previous][next][first][last][top][bottom][index][help] */