Menu

[b84aab]: / src / common / tasks.c  Maximize  Restore  History

Download this file

197 lines (174 with data), 3.4 kB

  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
// This file is part of SmallBASIC
//
// Task manager
//
// This program is distributed under the terms of the GPL v2.0 or later
// Download the GNU Public License (GPL) from www.gnu.org
//
// Copyright(C) 2000 Nicholas Christopoulos
#include "common/smbas.h"
#include "common/tasks.h"
static task_t *tasks; /**< tasks table @ingroup sys */
static int task_count; /**< total number of tasks @ingroup sys */
static int task_index; /**< current task number @ingroup sys */
/**
* @ingroup sys
*
* return the number of the tasks
*
* @return the number of the tasks
*/
int count_tasks() {
return task_count;
}
/**
* @ingroup sys
*
* return the active task-id
*
* @return the active task-id
*/
int current_tid() {
return task_index;
}
/**
* @ingroup sys
*
* return the active task-structure
*
* @return the active task-structure
*/
task_t *current_task() {
return &tasks[task_index];
}
/**
* @ingroup sys
*
* return the nth task-structure
*
* @return the nth task-structure
*/
task_t *taskinfo(int n) {
return &tasks[n];
}
/**
* @ingroup sys
*
* initialize tasks manager
*/
int init_tasks() {
int tid;
tasks = NULL;
task_count = 0;
task_index = 0;
ctask = NULL;
tid = create_task("main");
activate_task(tid);
return tid;
}
/**
* @ingroup sys
*
* destroys tasks and closes task manager
*/
void destroy_tasks() {
int i;
for (i = 0; i < task_count; i++)
close_task(i);
task_count = 0;
task_index = 0;
ctask = NULL;
tmp_free(tasks);
tasks = NULL;
}
/**
* @ingroup sys
*
* create an empty new task
*
* @param name is the task name
* @return the task-id
*/
int create_task(const char *name) {
int tid = -1;
int i;
if (task_count == 0) {
// this is the first task
tid = task_count;
task_count++;
tasks = (task_t *) tmp_alloc(sizeof(task_t) * task_count);
} else {
// search for an available free entry
for (i = 0; i < task_count; i++) {
if (tasks[i].status == tsk_free) {
tid = i;
break;
}
}
// create a new task
if (tid == -1) {
tid = task_count;
task_count++;
tasks = (task_t *) tmp_realloc(tasks, sizeof(task_t) * task_count);
}
}
// init task
memset(&tasks[tid], 0, sizeof(task_t));
strcpy(tasks[tid].file, name);
tasks[tid].status = tsk_ready;
tasks[tid].parent = task_index;
tasks[tid].tid = tid;
return tid;
}
/**
* @ingroup sys
*
* closes a task and activate the next
*/
void close_task(int tid) {
// memset(&tasks[tid], 0, sizeof(task_t));
tasks[tid].status = tsk_free;
if (task_index == tid)
ctask = NULL;
// select next task
if (task_count) {
if (task_index == tid) {
if (task_count > task_index + 1)
task_index++;
else
task_index = 0;
ctask = &tasks[task_index];
}
}
}
/**
* @ingroup sys
*
* set active task
*
* @param tid the task-id
* @return the previous task-id
*/
int activate_task(int tid) {
int prev_tid;
prev_tid = task_index;
task_index = tid;
ctask = &tasks[tid];
return prev_tid;
}
/**
* @ingroup sys
*
* search for a task
*
* @param task_name the name of the task
* @return the task-id; or -1 on error
*/
int search_task(const char *task_name) {
int i;
for (i = 0; i < task_count; i++) {
if (strcmp(tasks[i].file, task_name) == 0)
return i;
}
return -1;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.