2020-11-11 01:19:49 +03:00
|
|
|
/*
|
|
|
|
* MIT License
|
|
|
|
*
|
2021-02-05 20:46:59 +03:00
|
|
|
* Copyright (c) 2021 Ozan Tezcan
|
2020-11-11 01:19:49 +03:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
2021-02-10 21:33:03 +03:00
|
|
|
#ifndef _XOPEN_SOURCE
|
|
|
|
#define _XOPEN_SOURCE 700
|
|
|
|
#endif
|
2020-11-11 01:19:49 +03:00
|
|
|
|
|
|
|
#include "sc_mutex.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
|
|
|
int sc_mutex_init(struct sc_mutex *mtx)
|
|
|
|
{
|
|
|
|
InitializeCriticalSection(&mtx->mtx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sc_mutex_term(struct sc_mutex *mtx)
|
|
|
|
{
|
|
|
|
DeleteCriticalSection(&mtx->mtx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sc_mutex_lock(struct sc_mutex *mtx)
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&mtx->mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sc_mutex_unlock(struct sc_mutex *mtx)
|
|
|
|
{
|
|
|
|
LeaveCriticalSection(&mtx->mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int sc_mutex_init(struct sc_mutex *mtx)
|
|
|
|
{
|
2020-11-16 03:58:37 +03:00
|
|
|
int rc, rv;
|
2020-11-11 01:19:49 +03:00
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
mtx->mtx = mut;
|
|
|
|
|
|
|
|
// May fail on OOM
|
|
|
|
rc = pthread_mutexattr_init(&attr);
|
|
|
|
if (rc != 0) {
|
2020-11-29 23:21:25 +03:00
|
|
|
return -1;
|
2020-11-11 01:19:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This won't fail as long as we pass correct params.
|
|
|
|
rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
// May fail on OOM
|
|
|
|
rc = pthread_mutex_init(&mtx->mtx, &attr);
|
|
|
|
|
|
|
|
// This won't fail as long as we pass correct param.
|
2020-11-16 03:58:37 +03:00
|
|
|
rv = pthread_mutexattr_destroy(&attr);
|
|
|
|
assert(rv == 0);
|
2021-02-16 04:23:08 +03:00
|
|
|
(void) rv;
|
2020-11-16 03:58:37 +03:00
|
|
|
|
2020-11-29 23:21:25 +03:00
|
|
|
return rc != 0 ? -1 : 0;
|
2020-11-11 01:19:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int sc_mutex_term(struct sc_mutex *mtx)
|
|
|
|
{
|
2020-11-16 03:58:37 +03:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = pthread_mutex_destroy(&mtx->mtx);
|
2020-11-29 23:21:25 +03:00
|
|
|
return rc != 0 ? -1 : 0;
|
2020-11-11 01:19:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void sc_mutex_lock(struct sc_mutex *mtx)
|
|
|
|
{
|
2020-11-16 03:58:37 +03:00
|
|
|
int rc;
|
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
// This won't fail as long as we pass correct param.
|
2020-11-16 03:58:37 +03:00
|
|
|
rc = pthread_mutex_lock(&mtx->mtx);
|
|
|
|
assert(rc == 0);
|
2021-02-16 04:23:08 +03:00
|
|
|
(void) rc;
|
2020-11-11 01:19:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void sc_mutex_unlock(struct sc_mutex *mtx)
|
|
|
|
{
|
2020-11-16 03:58:37 +03:00
|
|
|
int rc;
|
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
// This won't fail as long as we pass correct param.
|
2020-11-16 03:58:37 +03:00
|
|
|
rc = pthread_mutex_unlock(&mtx->mtx);
|
|
|
|
assert(rc == 0);
|
2021-02-16 04:23:08 +03:00
|
|
|
(void) rc;
|
2020-11-11 01:19:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|