-
Notifications
You must be signed in to change notification settings - Fork 2
/
bsp_thr.c
118 lines (78 loc) · 2.08 KB
/
bsp_thr.c
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* BSP_thr.c
*
* Created on: 2015-1-6
* Author: ltp
*/
#include "bsp_thr.h"
#include "bsp_debug.h"
int BSP_thrCreate(BSP_ThrHndl *hndl, BSP_ThrEntryFunc entryFunc, int pri, unsigned int stackSize, void *prm)
{
int status=0;
pthread_attr_t thread_attr;
struct sched_param schedprm;
// initialize thread attributes structure
status = pthread_attr_init(&thread_attr);
if(status!=0) {
BSP_ERROR("BSP_thrCreate() - Could not initialize thread attributes\n");
return status;
}
if(stackSize!=BSP_THR_STACK_SIZE_DEFAULT)
pthread_attr_setstacksize(&thread_attr, stackSize);
if(pri != BSP_THR_PRI_DEFAULT)
{
status |= pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED);
status |= pthread_attr_setschedpolicy(&thread_attr, SCHED_FIFO);
if(pri>BSP_THR_PRI_MAX)
pri=BSP_THR_PRI_MAX;
else
if(pri<BSP_THR_PRI_MIN)
pri=BSP_THR_PRI_MIN;
schedprm.sched_priority = pri;
status |= pthread_attr_setschedparam(&thread_attr, &schedprm);
if(status!=0) {
BSP_ERROR("BSP_thrCreate() - Could not initialize thread attributes\n");
goto error_exit;
}
}
status = pthread_create(&hndl->hndl, &thread_attr, entryFunc, prm);
if(status!=0) {
BSP_ERROR("BSP_thrCreate() - Could not create thread [%d]\n", status);
perror("pthread_create");
}
error_exit:
pthread_attr_destroy(&thread_attr);
return status;
}
int BSP_thrJoin(BSP_ThrHndl *hndl)
{
int status=0;
void *returnVal;
status = pthread_join(hndl->hndl, &returnVal);
return status;
}
int BSP_thrDelete(BSP_ThrHndl *hndl)
{
int status=0;
status |= pthread_cancel(hndl->hndl);
status |= BSP_thrJoin(hndl);
return status;
}
int BSP_thrChangePri(BSP_ThrHndl *hndl, int pri)
{
int status = 0;
struct sched_param schedprm;
if(pri>BSP_THR_PRI_MAX)
pri=BSP_THR_PRI_MAX;
else
if(pri<BSP_THR_PRI_MIN)
pri=BSP_THR_PRI_MIN;
schedprm.sched_priority = pri;
status |= pthread_setschedparam(hndl->hndl, SCHED_FIFO, &schedprm);
return status;
}
int BSP_thrExit(void *returnVal)
{
pthread_exit(returnVal);
return 0;
}