1 /*
2  * Copyright (C) 2019 Codership Oy <info@codership.com>
3  *
4  * This file is part of wsrep-lib.
5  *
6  * Wsrep-lib is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Wsrep-lib is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with wsrep-lib.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 
21 /** @file thread_service.hpp
22  *
23  * Service interface for threads and synchronization primitives.
24  * The purpose of this interface is to provider provider implementations
25  * means to integrate with application thread implementation.
26  *
27  * Interface is designed to resemble POSIX threads, mutexes and
28  * condition variables.
29  */
30 
31 #ifndef WSREP_THREAD_SERVICE_HPP
32 #define WSREP_THREAD_SERVICE_HPP
33 
34 #include <cstddef> // size_t
35 #include "compiler.hpp"
36 
37 struct timespec;
38 struct sched_param;
39 
40 namespace wsrep
41 {
42 
43     class thread_service
44     {
45     public:
46 
thread_service()47         thread_service() : exit() { }
~thread_service()48         virtual ~thread_service() { }
49         struct thread_key { };
50         struct thread { };
51         struct mutex_key { };
52         struct mutex { };
53         struct cond_key { };
54         struct cond { };
55 
56         /**
57          * Method will be called before library side thread
58          * service initialization.
59          */
60         virtual int before_init() = 0;
61 
62         /**
63          * Method will be called after library side thread service
64          * has been initialized.
65          */
66         virtual int after_init() = 0;
67 
68         /* Thread */
69         virtual const thread_key* create_thread_key(const char* name) WSREP_NOEXCEPT
70             = 0;
71         virtual int create_thread(const thread_key*, thread**,
72                                   void* (*fn)(void*), void*) WSREP_NOEXCEPT
73             = 0;
74         virtual int detach(thread*) WSREP_NOEXCEPT = 0;
75         virtual int equal(thread*, thread*) WSREP_NOEXCEPT = 0;
76 
77         /*
78          * This unlike others is a function pointer to
79          * avoid having C++ methods on thread exit codepath.
80          */
81         WSREP_NORETURN void (*exit)(thread*, void* retval);
82         virtual int join(thread*, void** retval) WSREP_NOEXCEPT = 0;
83         virtual thread* self() WSREP_NOEXCEPT = 0;
84         virtual int setschedparam(thread*, int,
85                                   const struct sched_param*) WSREP_NOEXCEPT
86             = 0;
87         virtual int getschedparam(thread*, int*, struct sched_param*) WSREP_NOEXCEPT
88             = 0;
89 
90         /* Mutex */
91         virtual const mutex_key* create_mutex_key(const char* name) WSREP_NOEXCEPT
92             = 0;
93         virtual mutex* init_mutex(const mutex_key*, void*, size_t) WSREP_NOEXCEPT = 0;
94         virtual int destroy(mutex*) WSREP_NOEXCEPT = 0;
95         virtual int lock(mutex*) WSREP_NOEXCEPT = 0;
96         virtual int trylock(mutex*) WSREP_NOEXCEPT = 0;
97         virtual int unlock(mutex*) WSREP_NOEXCEPT = 0;
98 
99         /* Condition variable */
100         virtual const cond_key* create_cond_key(const char* name) WSREP_NOEXCEPT = 0;
101         virtual cond* init_cond(const cond_key*, void*, size_t) WSREP_NOEXCEPT = 0;
102         virtual int destroy(cond*) WSREP_NOEXCEPT = 0;
103         virtual int wait(cond*, mutex*) WSREP_NOEXCEPT = 0;
104         virtual int timedwait(cond*, mutex*, const struct timespec*) WSREP_NOEXCEPT
105             = 0;
106         virtual int signal(cond*) WSREP_NOEXCEPT = 0;
107         virtual int broadcast(cond*) WSREP_NOEXCEPT = 0;
108     };
109 } // namespace wsrep
110 
111 #endif // WSREP_THREAD_SERVICE_HPP
112