uThreads  0.3.0
cwrapper.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright © 2015, 2016 Saman Barghi
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  *******************************************************************************/
17 
18 #ifndef UTHREADS_CWRAPPER_H_
19 #define UTHREADS_CWRAPPER_H_
20 
21 #include <pthread.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <inttypes.h>
25 
32 #ifdef __cplusplus
33 extern "C"{
34 #endif
35 #include <stdbool.h>
36 
37 /**************Cluster*************/
42 struct WCluster;
43 typedef struct WCluster WCluster;
45 WCluster* cluster_create();
47 void cluster_destroy(WCluster* cluster);
49 WCluster* cluster_get_default();//return default Cluster
51 WCluster* cluster_get_current();
53 uint64_t cluster_get_id(WCluster* cluster);
55 size_t cluster_get_number_of_kThreads(WCluster* cluster);
57 /**********************************/
58 
59 /*************kThread**************/
64 struct WkThread;
65 typedef struct WkThread WkThread;
67 WkThread* kThread_create(WCluster* cluster);
69 void kThread_destroy(WkThread* kt);
73 WkThread* kThread_get_current();
74 //if linux
76 pthread_t kThread_get_current_pthread_id(); //return pthread_t for current running thread
78 pthread_t kThread_get_pthread_id(WkThread* kt); //return pthread_t for the provided kThread
79 //endif linux
81 /**********************************/
82 
83 /*************uThread**************/
88 struct WuThread;
89 typedef struct WuThread WuThread;
91 WuThread* uThread_create(bool joinable);
93 void uThread_start(WuThread* ut, WCluster* cluster, void *func, void *arg1 , void* arg2, void* arg3);
95 void uThread_migrate(WCluster* cluster);
97 void uThread_terminate(WuThread* ut);
99 void uThread_yield();
101 bool uThread_join(WuThread* ut);
103 void uThread_detach(WuThread* ut);
105 uint64_t uThread_get_id(WuThread* ut);
107 WuThread* uThread_get_current();
111 /**********************************/
112 
113 /*************Connection************/
118 struct WConnection;
119 typedef struct WConnection WConnection;
121 WConnection* connection_create();
123 WConnection* connection_create_with_fd(int fd);
125 WConnection* connection_create_socket(int domain, int type, int protocol);
127 void connection_destroy(WConnection* c);
128 
129 
131 int connection_accept(WConnection* acceptor, WConnection *conn, struct sockaddr *addr, socklen_t *addrlen);
133 WConnection* connection_accept_connenction(WConnection* acceptor, struct sockaddr *addr, socklen_t *addrlen);
134 
135 
137 int connection_socket(WConnection* conn, int domain, int type, int protocol);
138 
140 int connection_listen(WConnection* conn, int backlog);
142 int connection_bind(WConnection* conn, const struct sockaddr *addr,socklen_t addrlen);
144 int connection_connect(WConnection* conn, const struct sockaddr *addr,socklen_t addrlen);
146 int connection_close(WConnection* conn);
147 
149 ssize_t connection_recv(WConnection* conn, void *buf, size_t len, int flags);
151 ssize_t connection_recvfrom(WConnection* conn, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
153 ssize_t connection_recvmsg(WConnection* conn, int sockfd, struct msghdr *msg, int flags);
155 int connection_recvmmsg(WConnection* conn, int sockfd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags, struct timespec *timeout);
156 
158 ssize_t connection_send(WConnection* conn, const void *buf, size_t len, int flags);
160 ssize_t connection_sendto(WConnection* conn, int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);
162 ssize_t connection_sendmsg(WConnection* conn, const struct msghdr *msg, int flags);
164 int connection_sendmmsg(WConnection* conn, int sockfd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags);
165 
167 ssize_t connection_read(WConnection* conn, void *buf, size_t count);
169 ssize_t connection_write(WConnection* conn, const void *buf, size_t count);
170 
171 
173 void connection_block_on_read(WConnection* conn);
174 
176 void connection_block_on_write(WConnection* conn);
177 
179 int connection_get_fd(WConnection* conn);
181 /**********************************/
182 
183 
184 /******************** Mutex *********************/
189 struct WMutex;
190 typedef struct WMutex WMutex;
192 WMutex* mutex_create();
194 void mutex_destroy(WMutex* mutex);
196 bool mutex_acquire(WMutex* mutex);
198 void mutex_release(WMutex* mutex);
200 /**********************************/
201 
202 /******************** OwnlerLock ****************/
207 struct WOwnerLock;
208 typedef struct WOwnerLock WOwnerLock;
210 WOwnerLock* ownerlock_create();
212 void ownerlock_destroy(WOwnerLock* olock);
214 uint64_t ownerlock_acquire(WOwnerLock* olock);
216 void ownerlock_release(WOwnerLock* olock);
218 /**********************************/
219 
220 /******************** ConditionVariable *********/
225 struct WConditionVariable;
226 typedef struct WConditionVariable WConditionVariable;
228 WConditionVariable* condition_variable_create();
230 void condition_variable_destroy(WConditionVariable* cv);
232 void condition_variable_wait(WConditionVariable* cv, WMutex* mutex);
234 void condition_variable_signal(WConditionVariable* cv, WMutex* mutex);
236 void condition_variable_signall_all(WConditionVariable* cv, WMutex* mutex);
238 bool condition_variable_empty(WConditionVariable* cv);
240 /**********************************/
241 
242 /******************** Semaphore ****************/
247 struct WSemaphore;
248 typedef struct WSemaphore WSemaphore;
250 WSemaphore* semaphore_create();
252 void semaphore_destroy(WSemaphore* sem);
254 bool semaphore_p(WSemaphore* sem);
256 void semaphore_v(WSemaphore* sem);
258 /**********************************/
259 
260 /******************** uThreadPool **************/
265 struct WuThreadPool;
266 typedef struct WuThreadPool WuThreadPool;
267 WuThreadPool* uthreadpool_create();
268 void uthreadpool_destory(WuThreadPool* utp);
269 void uthreadpool_execute(WuThreadPool* utp, WCluster* cluster, void *(*start_routine) (void *), void *arg);
271 /**********************************/
272 #ifdef __cplusplus
273 }
274 #endif
275 
276 #endif /* UTHREADS_CWRAPPER_H_ */
WkThread * kThread_create(WCluster *cluster)
Definition: cwrapper.cpp:38
WuThread * uThread_create(bool joinable)
Create a uThread with a given stack size.
Definition: cwrapper.cpp:50
WConditionVariable * condition_variable_create()
Definition: cwrapper.cpp:141
void condition_variable_wait(WConditionVariable *cv, WMutex *mutex)
Block uThread on the condition variable using the provided mutex.
Definition: cwrapper.cpp:143
ssize_t connection_send(WConnection *conn, const void *buf, size_t len, int flags)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: cwrapper.cpp:101
WCluster * cluster_get_current()
Definition: cwrapper.cpp:32
WMutex * mutex_create()
Definition: cwrapper.cpp:127
int connection_get_fd(WConnection *conn)
Definition: cwrapper.cpp:120
ssize_t connection_recvfrom(WConnection *conn, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: cwrapper.cpp:90
void ownerlock_destroy(WOwnerLock *olock)
Definition: cwrapper.cpp:135
int connection_connect(WConnection *conn, const struct sockaddr *addr, socklen_t addrlen)
Same as connect syscall.
Definition: cwrapper.cpp:84
ssize_t connection_write(WConnection *conn, const void *buf, size_t count)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: cwrapper.cpp:115
void condition_variable_signal(WConditionVariable *cv, WMutex *mutex)
Unblock one uThread waiting on the condition variable.
Definition: cwrapper.cpp:144
ssize_t connection_read(WConnection *conn, void *buf, size_t count)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: cwrapper.cpp:114
pthread_t kThread_get_current_pthread_id()
return the native hanlde for the kernel thread
Definition: cwrapper.cpp:43
bool semaphore_p(WSemaphore *sem)
Decrement the value of the Semaphore.
Definition: cwrapper.cpp:152
bool mutex_acquire(WMutex *mutex)
acquire the mutex
Definition: cwrapper.cpp:129
int connection_socket(WConnection *conn, int domain, int type, int protocol)
Same as socket syscall, set the fd for current connection.
Definition: cwrapper.cpp:78
void condition_variable_signall_all(WConditionVariable *cv, WMutex *mutex)
unblock all uThreads waiting on the condition variable
Definition: cwrapper.cpp:145
void ownerlock_release(WOwnerLock *olock)
Definition: cwrapper.cpp:137
uint64_t uThread_get_id(WuThread *ut)
get the ID of this uThread
Definition: cwrapper.cpp:59
void mutex_destroy(WMutex *mutex)
Definition: cwrapper.cpp:128
void semaphore_destroy(WSemaphore *sem)
Definition: cwrapper.cpp:151
uint64_t uThread_get_total_number_of_uThreads()
Definition: cwrapper.cpp:61
void mutex_release(WMutex *mutex)
release the Mutex
Definition: cwrapper.cpp:130
WConnection * connection_create_socket(int domain, int type, int protocol)
Same as socket syscall adds | SOCK_NONBLOCK to type.
Definition: cwrapper.cpp:68
WCluster * cluster_create()
Definition: cwrapper.cpp:29
void uThread_start(WuThread *ut, WCluster *cluster, void *func, void *arg1, void *arg2, void *arg3)
start the uThread by calling the function passed to it
Definition: cwrapper.cpp:51
void semaphore_v(WSemaphore *sem)
increment the value of the Semaphore
Definition: cwrapper.cpp:153
uint64_t cluster_get_id(WCluster *cluster)
Get the ID of Cluster.
Definition: cwrapper.cpp:33
WConnection * connection_accept_connenction(WConnection *acceptor, struct sockaddr *addr, socklen_t *addrlen)
Accepts a connection and returns a connection object.
Definition: cwrapper.cpp:74
void condition_variable_destroy(WConditionVariable *cv)
Definition: cwrapper.cpp:142
bool uThread_join(WuThread *ut)
Wait for uThread to finish execution and exit.
Definition: cwrapper.cpp:57
uint64_t kThread_get_total_number_of_kThreads()
Definition: cwrapper.cpp:40
ssize_t connection_sendto(WConnection *conn, int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: cwrapper.cpp:104
WkThread * kThread_get_current()
Get the pointer to the current kThread.
Definition: cwrapper.cpp:41
int connection_sendmmsg(WConnection *conn, int sockfd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: cwrapper.cpp:110
void connection_destroy(WConnection *c)
Definition: cwrapper.cpp:69
void uThread_migrate(WCluster *cluster)
Move the uThread to the provided cluster.
Definition: cwrapper.cpp:54
WCluster * cluster_get_default()
Definition: cwrapper.cpp:31
void uThread_terminate(WuThread *ut)
Terminates the uThread.
Definition: cwrapper.cpp:55
void cluster_destroy(WCluster *cluster)
Definition: cwrapper.cpp:30
bool condition_variable_empty(WConditionVariable *cv)
Whether the waiting list is empty or not.
Definition: cwrapper.cpp:146
int connection_close(WConnection *conn)
closes the socket
Definition: cwrapper.cpp:85
size_t cluster_get_number_of_kThreads(WCluster *cluster)
Total number of kThreads belonging to this cluster.
Definition: cwrapper.cpp:34
int connection_accept(WConnection *acceptor, WConnection *conn, struct sockaddr *addr, socklen_t *addrlen)
nonblocking accept syscall and updating the passed Connection object
Definition: cwrapper.cpp:71
WConnection * connection_create()
Create a Connection that does not have.
Definition: cwrapper.cpp:66
int connection_recvmmsg(WConnection *conn, int sockfd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags, struct timespec *timeout)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: cwrapper.cpp:96
WOwnerLock * ownerlock_create()
Definition: cwrapper.cpp:134
WSemaphore * semaphore_create()
Create a new Semaphore.
Definition: cwrapper.cpp:150
uint64_t ownerlock_acquire(WOwnerLock *olock)
Definition: cwrapper.cpp:136
int connection_bind(WConnection *conn, const struct sockaddr *addr, socklen_t addrlen)
Same as bind syscall.
Definition: cwrapper.cpp:83
int connection_listen(WConnection *conn, int backlog)
Same as listen syscall on current fd.
Definition: cwrapper.cpp:82
WuThread * uThread_get_current()
Get a pointer to the current running uThread.
Definition: cwrapper.cpp:60
void uThread_detach(WuThread *ut)
Detach a joinable thread.
Definition: cwrapper.cpp:58
void connection_block_on_write(WConnection *conn)
Block uThread, waiting for fd to become ready for write.
Definition: cwrapper.cpp:118
WConnection * connection_create_with_fd(int fd)
Create a connection object with the provided fd.
Definition: cwrapper.cpp:67
void connection_block_on_read(WConnection *conn)
Block uThread, waiting for fd to become ready for read.
Definition: cwrapper.cpp:117
pthread_t kThread_get_pthread_id(WkThread *kt)
return the native hanlde for the kernel thread
Definition: cwrapper.cpp:44
ssize_t connection_recvmsg(WConnection *conn, int sockfd, struct msghdr *msg, int flags)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: cwrapper.cpp:93
ssize_t connection_recv(WConnection *conn, void *buf, size_t len, int flags)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: cwrapper.cpp:87
void uThread_yield()
Causes uThread to yield.
Definition: cwrapper.cpp:56
ssize_t connection_sendmsg(WConnection *conn, const struct msghdr *msg, int flags)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: cwrapper.cpp:107
void kThread_destroy(WkThread *kt)
Definition: cwrapper.cpp:39