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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
| #ifndef OSAL_H #define OSAL_H
typedef void (*osal_task_func_t)(void *arg);
typedef struct { osal_task_func_t task_func; void *arg; } osal_task_t;
int osal_task_create(osal_task_t *task, const char *name); void osal_task_delete(osal_task_t *task);
typedef void* osal_mutex_t; osal_mutex_t osal_mutex_create(void); void osal_mutex_lock(osal_mutex_t mutex); void osal_mutex_unlock(osal_mutex_t mutex); void osal_mutex_delete(osal_mutex_t mutex);
typedef void* osal_timer_t; typedef void (*osal_timer_callback_t)(void*); osal_timer_t osal_timer_create(osal_timer_callback_t callback, void* arg, int period_ms, int is_periodic); void osal_timer_start(osal_timer_t timer); void osal_timer_stop(osal_timer_t timer); void osal_timer_delete(osal_timer_t timer);
typedef struct { void* message; size_t message_size; }osal_message_t;
typedef void* osal_queue_t; osal_queue_t osal_queue_create(size_t queue_size); int osal_queue_send(osal_queue_t queue, void* message, size_t message_size); int osal_queue_receive(osal_queue_t queue, void* message, size_t max_size); void osal_queue_delete(osal_queue_t queue);
#endif
#include "osal.h" #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <unistd.h> #include <pthread.h>
#define MAX_TASKS 10 #define MAX_TIMER 10 #define MAX_QUEUE 10
static osal_task_t tasks[MAX_TASKS]; static int task_count = 0; static bool is_system_run = true;
typedef struct{ osal_timer_callback_t callback; void* arg; int period_ms; int is_periodic; bool is_running; pthread_t thread_id; }osal_timer_impl_t;
static osal_timer_impl_t timers[MAX_TIMER]; static int timer_count = 0;
typedef struct{ void** buffer; size_t buffer_size; int write_index; int read_index; pthread_mutex_t lock; pthread_cond_t cond_read; pthread_cond_t cond_write; bool is_created; }osal_queue_impl_t;
static osal_queue_impl_t queues[MAX_QUEUE]; static int queue_count = 0;
int osal_task_create(osal_task_t *task, const char *name) { if (task_count >= MAX_TASKS) { return -1; } tasks[task_count] = *task; task_count++; printf("创建任务: %s\n",name); return 0; }
void osal_task_delete(osal_task_t *task) { for (int i = 0; i < task_count; ++i) { if (&tasks[i] == task) { for (int j = i; j < task_count - 1; ++j) { tasks[j] = tasks[j + 1]; } task_count--; printf("删除任务\n"); break; } } }
typedef struct { pthread_mutex_t lock; bool is_created; }osal_mutex_impl_t;
osal_mutex_t osal_mutex_create(void){ osal_mutex_impl_t* mutex = (osal_mutex_impl_t*) malloc(sizeof(osal_mutex_impl_t)); if(mutex == NULL){ return NULL; } if(pthread_mutex_init(&mutex->lock,NULL) !=0){ free(mutex); return NULL; } mutex->is_created = true; return (osal_mutex_t)mutex;
}
void osal_mutex_lock(osal_mutex_t mutex){ osal_mutex_impl_t* m = (osal_mutex_impl_t*)mutex; if (m == NULL || m->is_created == false){ return; } pthread_mutex_lock(&m->lock); }
void osal_mutex_unlock(osal_mutex_t mutex){ osal_mutex_impl_t* m = (osal_mutex_impl_t*)mutex; if (m == NULL || m->is_created == false){ return; } pthread_mutex_unlock(&m->lock); }
void osal_mutex_delete(osal_mutex_t mutex){ osal_mutex_impl_t* m = (osal_mutex_impl_t*)mutex; if (m == NULL || m->is_created == false){ return; } pthread_mutex_destroy(&m->lock); free(m); printf("删除互斥锁\n"); }
void* timer_thread(void* arg){ osal_timer_impl_t* timer = (osal_timer_impl_t*)arg;
while(timer->is_running){ usleep(timer->period_ms * 1000); if(timer->callback != NULL){ timer->callback(timer->arg); } if(!timer->is_periodic){ break; } } return NULL; }
osal_timer_t osal_timer_create(osal_timer_callback_t callback, void* arg, int period_ms, int is_periodic){ if(timer_count >= MAX_TIMER){ return NULL; } osal_timer_impl_t* timer = &timers[timer_count]; timer->callback = callback; timer->arg = arg; timer->period_ms = period_ms; timer->is_periodic = is_periodic; timer->is_running = false; timer_count++; printf("创建定时器,周期 %d ms\n",period_ms); return (osal_timer_t)timer; }
void osal_timer_start(osal_timer_t timer){ osal_timer_impl_t* t = (osal_timer_impl_t*)timer; if(t->is_running) return;
t->is_running = true; if(pthread_create(&t->thread_id,NULL,timer_thread,t) !=0){ t->is_running = false; } }
void osal_timer_stop(osal_timer_t timer){ osal_timer_impl_t* t = (osal_timer_impl_t*)timer; if(!t->is_running) return; t->is_running = false; pthread_join(t->thread_id,NULL); } void osal_timer_delete(osal_timer_t timer){ osal_timer_impl_t* t = (osal_timer_impl_t*)timer; if(t->is_running) return; for (int i = 0; i < timer_count; ++i) { if (&timers[i] == t) { for (int j = i; j < timer_count - 1; ++j) { timers[j] = timers[j + 1]; } timer_count--; printf("删除定时器\n"); break; } } }
osal_queue_t osal_queue_create(size_t queue_size){ if(queue_count >= MAX_QUEUE){ return NULL; } osal_queue_impl_t* queue = &queues[queue_count]; queue->buffer = malloc(sizeof(void*) * queue_size); if(queue->buffer == NULL) return NULL; queue->buffer_size = queue_size; queue->write_index = 0; queue->read_index = 0; pthread_mutex_init(&queue->lock,NULL); pthread_cond_init(&queue->cond_read, NULL); pthread_cond_init(&queue->cond_write, NULL); queue->is_created = true; queue_count++;
printf("创建消息队列,容量%d\n",queue_size); return (osal_queue_t)queue; }
int osal_queue_send(osal_queue_t queue, void* message, size_t message_size){ osal_queue_impl_t* q = (osal_queue_impl_t*)queue; if (q == NULL || q->is_created == false){ return -1; } pthread_mutex_lock(&q->lock);
while((q->write_index + 1) % q->buffer_size == q->read_index){ pthread_cond_wait(&q->cond_write, &q->lock); }
q->buffer[q->write_index] = malloc(message_size); if(q->buffer[q->write_index] == NULL){ pthread_mutex_unlock(&q->lock); return -1; } memcpy(q->buffer[q->write_index], message, message_size); q->write_index = (q->write_index + 1)%q->buffer_size;
pthread_cond_signal(&q->cond_read); pthread_mutex_unlock(&q->lock); printf("发送消息到队列\n"); return 0; }
int osal_queue_receive(osal_queue_t queue, void* message, size_t max_size){ osal_queue_impl_t* q = (osal_queue_impl_t*)queue; if (q == NULL || q->is_created == false){ return -1; } pthread_mutex_lock(&q->lock); while(q->write_index == q->read_index){ pthread_cond_wait(&q->cond_read, &q->lock); }
size_t copy_size = 0; if(max_size < 0) copy_size = 0; else{ copy_size = max_size; } if(q->buffer[q->read_index] != NULL){ memcpy(message, q->buffer[q->read_index], copy_size ); free(q->buffer[q->read_index]); q->buffer[q->read_index] = NULL; }
q->read_index = (q->read_index + 1)%q->buffer_size; pthread_cond_signal(&q->cond_write); pthread_mutex_unlock(&q->lock); printf("从队列接收消息\n"); return 0; }
void osal_queue_delete(osal_queue_t queue){ osal_queue_impl_t* q = (osal_queue_impl_t*)queue; if (q == NULL || q->is_created == false){ return; }
pthread_mutex_destroy(&q->lock); pthread_cond_destroy(&q->cond_read); pthread_cond_destroy(&q->cond_write); for(int i=0; i<q->buffer_size;i++){ if(q->buffer[i]!= NULL){ free(q->buffer[i]); q->buffer[i] = NULL; } } free(q->buffer);
for (int i = 0; i < queue_count; ++i) { if (&queues[i] == q) { for (int j = i; j < queue_count - 1; ++j) { queues[j] = queues[j + 1]; } queue_count--; printf("删除消息队列\n"); break; } } }
void osal_run() { printf("OSAL 运行...\n"); while (is_system_run) { for (int i = 0; i < task_count; ++i) { if (tasks[i].task_func != NULL) { tasks[i].task_func(tasks[i].arg); } } usleep(1000); } }
void osal_stop(){ is_system_run = false; }
|