-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.h
executable file
·59 lines (53 loc) · 1.3 KB
/
header.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef _SAAD_HEADER_H_
#define _SAAD_HEADER_H_
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <syscall.h>
/*
* @brief print error msg and exit process with status 1
* @param the error message to print
*/
void err_quit(const char *err_msg)
{
perror(err_msg);
exit(1);
}
/*
* @brief creates a new thread of default attributes
* @param routine poitner to routine function
* @param argument pointer to argument to pass in routine. NULL if none
* @returns the thread id
* @throws exits process if thread creation fails
*/
pthread_t Pthread_create(void *(*routine)(void *), void *argument)
{
pthread_t thid;
if (pthread_create(&thid, NULL, routine, (void *)argument))
err_quit("Pthread_create() failed");
return thid;
}
/*
* @brief waits for a thread to complete
* @param the thread id
* @returns the returned value by thread. 0 if none
* @throws exits process if thread joining fails
*/
intptr_t Pthread_join(pthread_t thid)
{
void *status;
if (pthread_join(thid, &status))
err_quit("Pthread_join() failed");
return (intptr_t)status;
}
/*
* @brief exits the thread and cleans up resources
* @param the return value
*/
void Pthread_exit(intptr_t retval)
{
pthread_exit((void *)retval);
}
#endif