Welcome to mirror list, hosted at ThFree Co, Russian Federation.

thread.c « furi « core - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de86cbdc67f9e30b720b2a39448d0aed278bb715 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "thread.h"
#include "memmgr.h"
#include "memmgr_heap.h"
#include "check.h"

#include <m-string.h>

struct FuriThread {
    FuriThreadState state;
    int32_t ret;

    FuriThreadCallback callback;
    void* context;

    FuriThreadStateCallback state_callback;
    void* state_context;

    osThreadAttr_t attr;
    volatile osThreadId_t id;

    bool heap_trace_enabled;
    size_t heap_size;
};

void furi_thread_set_state(FuriThread* thread, FuriThreadState state) {
    furi_assert(thread);
    thread->state = state;
    if(thread->state_callback) {
        thread->state_callback(state, thread->state_context);
    }
}

void furi_thread_body(void* context) {
    furi_assert(context);
    FuriThread* thread = context;

    furi_assert(thread->state == FuriThreadStateStarting);
    furi_thread_set_state(thread, FuriThreadStateRunning);

    osThreadId_t thread_id = osThreadGetId();
    if(thread->heap_trace_enabled == true) {
        memmgr_heap_enable_thread_trace(thread_id);
    }

    thread->ret = thread->callback(thread->context);

    if(thread->heap_trace_enabled == true) {
        thread->heap_size = memmgr_heap_get_thread_memory(thread_id);
        memmgr_heap_disable_thread_trace(thread_id);
    }

    furi_assert(thread->state == FuriThreadStateRunning);
    furi_thread_set_state(thread, FuriThreadStateStopped);

    osThreadExit();
}

FuriThread* furi_thread_alloc() {
    FuriThread* thread = malloc(sizeof(FuriThread));

    return thread;
}

void furi_thread_free(FuriThread* thread) {
    furi_assert(thread);
    furi_assert(thread->state == FuriThreadStateStopped);

    if(thread->attr.name) free((void*)thread->attr.name);
    free(thread);
}

void furi_thread_set_name(FuriThread* thread, const char* name) {
    furi_assert(thread);
    furi_assert(thread->state == FuriThreadStateStopped);
    if(thread->attr.name) free((void*)thread->attr.name);
    thread->attr.name = strdup(name);
}

void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size) {
    furi_assert(thread);
    furi_assert(thread->state == FuriThreadStateStopped);
    thread->attr.stack_size = stack_size;
}

void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback) {
    furi_assert(thread);
    furi_assert(thread->state == FuriThreadStateStopped);
    thread->callback = callback;
}

void furi_thread_set_context(FuriThread* thread, void* context) {
    furi_assert(thread);
    furi_assert(thread->state == FuriThreadStateStopped);
    thread->context = context;
}

void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback) {
    furi_assert(thread);
    furi_assert(thread->state == FuriThreadStateStopped);
    thread->state_callback = callback;
}

void furi_thread_set_state_context(FuriThread* thread, void* context) {
    furi_assert(thread);
    furi_assert(thread->state == FuriThreadStateStopped);
    thread->state_context = context;
}

FuriThreadState furi_thread_get_state(FuriThread* thread) {
    furi_assert(thread);
    return thread->state;
}

bool furi_thread_start(FuriThread* thread) {
    furi_assert(thread);
    furi_assert(thread->callback);
    furi_assert(thread->state == FuriThreadStateStopped);
    furi_assert(thread->attr.stack_size > 0);
    furi_thread_set_state(thread, FuriThreadStateStarting);
    thread->id = osThreadNew(furi_thread_body, thread, &thread->attr);
    if(thread->id) {
        return true;
    } else {
        furi_assert(thread->state == FuriThreadStateStarting);
        furi_thread_set_state(thread, FuriThreadStateStopped);
        return false;
    }
}

osStatus_t furi_thread_terminate(FuriThread* thread) {
    furi_assert(thread);
    osStatus_t ret = osThreadTerminate(thread->id);
    if(ret == osOK) {
        furi_thread_set_state(thread, FuriThreadStateStopped);
    }
    return ret;
}

osStatus_t furi_thread_join(FuriThread* thread) {
    furi_assert(thread);
    while(thread->state != FuriThreadStateStopped) {
        osDelay(10);
    }
    return osOK;
}

osThreadId_t furi_thread_get_thread_id(FuriThread* thread) {
    return thread->id;
}

void furi_thread_enable_heap_trace(FuriThread* thread) {
    furi_assert(thread);
    furi_assert(thread->state == FuriThreadStateStopped);
    furi_assert(thread->heap_trace_enabled == false);
    thread->heap_trace_enabled = true;
}

void furi_thread_disable_heap_trace(FuriThread* thread) {
    furi_assert(thread);
    furi_assert(thread->state == FuriThreadStateStopped);
    furi_assert(thread->heap_trace_enabled == true);
    thread->heap_trace_enabled = false;
}

size_t furi_thread_get_heap_size(FuriThread* thread) {
    furi_assert(thread);
    furi_assert(thread->heap_trace_enabled == true);
    return thread->heap_size;
}