SDL

Форк
0
/
SDL_sysrwlock.cpp 
117 строк · 3.4 Кб
1
/*
2
  Simple DirectMedia Layer
3
  Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4

5
  This software is provided 'as-is', without any express or implied
6
  warranty.  In no event will the authors be held liable for any damages
7
  arising from the use of this software.
8

9
  Permission is granted to anyone to use this software for any purpose,
10
  including commercial applications, and to alter it and redistribute it
11
  freely, subject to the following restrictions:
12

13
  1. The origin of this software must not be misrepresented; you must not
14
     claim that you wrote the original software. If you use this software
15
     in a product, an acknowledgment in the product documentation would be
16
     appreciated but is not required.
17
  2. Altered source versions must be plainly marked as such, and must not be
18
     misrepresented as being the original software.
19
  3. This notice may not be removed or altered from any source distribution.
20
*/
21
#include "SDL_internal.h"
22

23
#include <shared_mutex>
24
#include <system_error>
25
#include <Windows.h>
26

27
struct SDL_RWLock
28
{
29
    std::shared_mutex cpp_mutex;
30
    SDL_ThreadID write_owner;
31
};
32

33
extern "C"
34
SDL_RWLock *SDL_CreateRWLock(void)
35
{
36
    try {
37
        SDL_RWLock *rwlock = new SDL_RWLock;
38
        return rwlock;
39
    } catch (std::system_error &ex) {
40
        SDL_SetError("unable to create a C++ rwlock: code=%d; %s", ex.code(), ex.what());
41
        return NULL;
42
    } catch (std::bad_alloc &) {
43
        SDL_OutOfMemory();
44
        return NULL;
45
    }
46
}
47

48
extern "C"
49
void SDL_DestroyRWLock(SDL_RWLock *rwlock)
50
{
51
    if (rwlock) {
52
        delete rwlock;
53
    }
54
}
55

56
extern "C"
57
void SDL_LockRWLockForReading(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS  // clang doesn't know about NULL mutexes
58
{
59
    if (rwlock) {
60
        try {
61
            rwlock->cpp_mutex.lock_shared();
62
        } catch (std::system_error &/*ex*/) {
63
            SDL_assert(!"Error trying to lock rwlock for reading");  // assume we're in a lot of trouble if this assert fails.
64
            //return SDL_SetError("unable to lock a C++ rwlock: code=%d; %s", ex.code(), ex.what());
65
        }
66
    }
67
}
68

69
extern "C"
70
void SDL_LockRWLockForWriting(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
71
{
72
    if (rwlock) {
73
        try {
74
            rwlock->cpp_mutex.lock();
75
            rwlock->write_owner = SDL_GetCurrentThreadID();
76
        } catch (std::system_error &/*ex*/) {
77
            SDL_assert(!"Error trying to lock rwlock for writing");  // assume we're in a lot of trouble if this assert fails.
78
            //return SDL_SetError("unable to lock a C++ rwlock: code=%d; %s", ex.code(), ex.what());
79
        }
80
    }
81
}
82

83
extern "C"
84
SDL_bool SDL_TryLockRWLockForReading(SDL_RWLock *rwlock)
85
{
86
    bool result = true;
87
    if (rwlock) {
88
        result = rwlock->cpp_mutex.try_lock_shared();
89
    }
90
    return result;
91
}
92

93
extern "C"
94
SDL_bool SDL_TryLockRWLockForWriting(SDL_RWLock *rwlock)
95
{
96
    bool result = true;
97
    if (rwlock) {
98
        result = rwlock->cpp_mutex.try_lock();
99
        if (result) {
100
            rwlock->write_owner = SDL_GetCurrentThreadID();
101
        }
102
    }
103
    return result;
104
}
105

106
extern "C"
107
void SDL_UnlockRWLock(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS  // clang doesn't know about NULL mutexes
108
{
109
    if (rwlock) {
110
        if (rwlock->write_owner == SDL_GetCurrentThreadID()) {
111
            rwlock->write_owner = 0;
112
            rwlock->cpp_mutex.unlock();
113
        } else {
114
            rwlock->cpp_mutex.unlock_shared();
115
        }
116
    }
117
}
118

119

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.