Skip to content

Commit

Permalink
Merge pull request #1 from discosat/dynamically-allocated-store
Browse files Browse the repository at this point in the history
Add dynamically allocated proc store and locks
  • Loading branch information
NValsted authored Apr 14, 2024
2 parents 6a4522c + 5a4c899 commit ef14513
Show file tree
Hide file tree
Showing 12 changed files with 447 additions and 77 deletions.
49 changes: 49 additions & 0 deletions include/csp_proc/proc_mutex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef CSP_PROC_MUTEX_H
#define CSP_PROC_MUTEX_H

#ifdef __cplusplus
extern "C" {
#endif

#define PROC_MUTEX_OK 0
#define PROC_MUTEX_ERR 1

typedef struct proc_mutex_t proc_mutex_t;

/**
* Create a new mutex.
*
* @return A pointer to the new mutex
*/
proc_mutex_t * proc_mutex_create();

/**
* Take/lock mutex.
*
* @param mutex The mutex to take
*
* @return 0 on success, 1 on failure
*/
int proc_mutex_take(proc_mutex_t * mutex);

/**
* Give/unlock mutex.
*
* @param mutex The mutex to give
*
* @return 0 on success, 1 on failure
*/
int proc_mutex_give(proc_mutex_t * mutex);

/**
* Destroy mutex.
*
* @param mutex The mutex to destroy
*/
void proc_mutex_destroy(proc_mutex_t * mutex);

#ifdef __cplusplus
}
#endif

#endif // CSP_PROC_MUTEX_H
4 changes: 2 additions & 2 deletions include/csp_proc/proc_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extern "C" {
*
* @return 0 on success, -1 on failure
*/
int proc_runtime_init();
int __attribute__((weak)) proc_runtime_init();

/**
* Run a procedure stored in a given slot.
Expand All @@ -43,7 +43,7 @@ int proc_runtime_init();
*
* @return 0 on success, -1 on failure
*/
int proc_runtime_run(uint8_t proc_slot);
int __attribute__((weak)) proc_runtime_run(uint8_t proc_slot);

/**
* Used to indicate the result of an if-else instruction in an instruction handler.
Expand Down
7 changes: 7 additions & 0 deletions include/csp_proc/proc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ typedef enum {
#define PROC_FLAG_ERROR_MASK 0b01000000
#define PROC_FLAG_ERROR 0b01000000

/**
* Conditionally initialize sub-components of the procedure server (proc_store, proc_runtime)
*
* @return 0 on success, -1 on failure
*/
int proc_server_init();

/**
* Handle incoming procedure requests
*
Expand Down
20 changes: 13 additions & 7 deletions include/csp_proc/proc_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@ extern "C" {
#endif

#include <stdint.h>
#include <csp_proc/proc_mutex.h>
#include <csp_proc/proc_types.h>

extern proc_t proc_store[MAX_PROC_SLOT + 1];
extern proc_mutex_t * proc_store_mutex;

/**
* Delete/reinitialize a fresh procedure from the procedure storage.
*
* @param slot The slot to delete the procedure from
*/
int delete_proc(uint8_t slot);
int __attribute__((weak)) delete_proc(uint8_t slot);

/**
* Initialize/reset the procedure storage.
* Reset the procedure storage.
*/
void reset_proc_store();
void __attribute__((weak)) reset_proc_store();

/**
* Initialize the procedure storage and any necessary resources.
*/
int __attribute__((weak)) proc_store_init();

/**
* Add a procedure to the procedure storage at the specified slot.
Expand All @@ -31,7 +37,7 @@ void reset_proc_store();
*
* @return The slot the procedure was added to, or -1 if the slot was occupied and overwrite was false
*/
int set_proc(proc_t * proc, uint8_t slot, int overwrite);
int __attribute__((weak)) set_proc(proc_t * proc, uint8_t slot, int overwrite);

/**
* Get a procedure from the procedure storage.
Expand All @@ -40,14 +46,14 @@ int set_proc(proc_t * proc, uint8_t slot, int overwrite);
*
* @return The procedure at the specified slot, or NULL if the slot is empty
*/
proc_t * get_proc(uint8_t slot);
proc_t * __attribute__((weak)) get_proc(uint8_t slot);

/**
* Get the slots of the procedures in the procedure storage with an instruction count greater than 0.
*
* @return A dynamically allocated array containing the slots of the procedures, terminated by -1. The caller is responsible for freeing this array.
*/
int * get_proc_slots();
int * __attribute__((weak)) get_proc_slots();

#ifdef __cplusplus
}
Expand Down
52 changes: 46 additions & 6 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ csp_proc_inc = include_directories('include')
csp_proc_src = files([
'src/proc_client.c',
'src/proc_pack.c',
'src/proc_server.c',
'src/proc_store.c',
])

# Static & dynamic analysis
Expand All @@ -35,12 +33,28 @@ if get_option('slash') == true
endif

if get_option('posix') == true and get_option('freertos') == true
error('Cannot build with multiple runtimes enabled (choose one max)')
error('Cannot build for both POSIX and FreeRTOS (choose one max)')
endif

if get_option('posix') == false and get_option('freertos') == false
error('Must build for either POSIX or FreeRTOS (choose one)')
endif

if get_option('posix') == true
csp_proc_src += files([
'src/sync/proc_mutex_POSIX.c',
])
endif

if get_option('freertos') == true
csp_proc_src += files([
'src/sync/proc_mutex_FreeRTOS.c',
])
endif

# Default FreeRTOS-based runtime implementation
freertos_dep = []
if get_option('freertos') == true
if get_option('freertos') == true and get_option('proc_runtime') == true
freertos_dep = dependency('freertos', fallback : ['freertos', 'freertos_dep'], required: false)
if freertos_dep.found()
csp_proc_src += files([
Expand All @@ -50,17 +64,43 @@ if get_option('freertos') == true
'src/proc_analyze.c',
])
endif
if get_option('proc_store_static') == false and get_option('proc_store_dynamic') == false
error('FreeRTOS runtime requires a proc store (e.g. proc_store_dynamic=true)')
endif
endif

# Default POSIX-based runtime implementation
posix_dep = []
if get_option('posix') == true
if get_option('posix') == true and get_option('proc_runtime') == true
csp_proc_src += files([
'src/runtime/proc_runtime_instructions_common.c',
'src/runtime/proc_runtime_instructions_POSIX.c',
'src/runtime/proc_runtime_POSIX.c',
'src/proc_analyze.c',
])
if get_option('proc_store_static') == false and get_option('proc_store_dynamic') == false
error('POSIX runtime requires a proc store (e.g. proc_store_dynamic=true)')
endif
endif

if get_option('proc_store_static') == true and get_option('proc_store_dynamic') == true
error('Cannot build with multiple proc stores enabled (choose one max)')
endif

# Static store
if get_option('proc_store_static') == true
csp_proc_src += files([
'src/store/proc_store_static.c',
'src/proc_server.c', # proc server only makes sense with a proc store
])
endif

# Dynamic store
if get_option('proc_store_dynamic') == true
csp_proc_src += files([
'src/store/proc_store_dynamic.c',
'src/proc_server.c', # proc server only makes sense with a proc store
])
endif

# Configuration options
Expand Down Expand Up @@ -95,7 +135,7 @@ csp_proc_lib = static_library('csp_proc',
install : false
)

csp_proc_dep = declare_dependency(include_directories : csp_proc_inc, link_with : csp_proc_lib)
csp_proc_dep = declare_dependency(include_directories : csp_proc_inc, link_with : csp_proc_lib, dependencies : [csp_dep, param_dep])


# Tests
Expand Down
9 changes: 6 additions & 3 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
option('build_tests', type: 'boolean', value: false, description: 'Build the test suite')

option('slash', type: 'boolean', value: false, description: 'Build slash (yields)', yield: true)
option('freertos', type: 'boolean', value: false, description: 'Build the default FreeRTOS-based runtime (yields)', yield: true)
option('posix', type: 'boolean', value: false, description: 'Build the default POSIX-based runtime (yields)', yield: true)
option('slash', type: 'boolean', value: false, description: 'Build slash', yield: true)
option('freertos', type: 'boolean', value: false, description: 'Build for FreeRTOS system', yield: true)
option('posix', type: 'boolean', value: true, description: 'Build for POSIX system', yield: true)
option('proc_runtime', type: 'boolean', value: false, description: 'Build the runtime module')
option('proc_analysis', type: 'boolean', value: false, description: 'Build the analysis module')
option('proc_store_static', type: 'boolean', value: false, description: 'Build the proc store with static memory allocation')
option('proc_store_dynamic', type: 'boolean', value: true, description: 'Build the proc store with dynamic memory allocation')

option('RESERVED_PROC_SLOTS', type : 'string', value : '', description : 'The number of reserved procedure slots.')
option('MAX_PROC_BLOCK_TIMEOUT_MS', type : 'string', value : '', description : 'The maximum time block instructions will wait before timing out.')
Expand Down
37 changes: 34 additions & 3 deletions src/proc_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,29 @@

#include <stdlib.h>
#include <csp/csp_types.h>
#include <csp/csp.h>

int __attribute__((weak)) proc_runtime_run(uint8_t proc_slot);
int proc_server_init() {
int ret = 0;

if (proc_store_init != NULL) {
csp_print("Initializing proc store\n");
ret = proc_store_init();
if (ret != 0) {
return ret;
}
}

if (proc_runtime_init != NULL) {
csp_print("Initializing proc runtime\n");
ret = proc_runtime_init();
if (ret != 0) {
return ret;
}
}
csp_print("Proc server initialized\n");
return ret;
}

static void proc_serve_del_request(csp_packet_t * packet) {
uint8_t slot = packet->data[1];
Expand All @@ -22,7 +43,7 @@ static void proc_serve_del_request(csp_packet_t * packet) {
static void proc_serve_pull_request(csp_packet_t * packet) {
uint8_t slot = packet->data[1];
proc_t * procedure = get_proc(slot);
if (procedure == NULL || procedure->instruction_count == 0) {
if (procedure == NULL) {
printf("Procedure not found\n");
packet->data[0] = PROC_PULL_RESPONSE;
packet->data[0] |= PROC_FLAG_END;
Expand Down Expand Up @@ -72,7 +93,17 @@ static void proc_serve_push_request(csp_packet_t * packet) {
return;
}

set_proc(procedure, packet->data[1], 1);
ret = set_proc(procedure, packet->data[1], 0);
if (ret < 0) {
printf("Failed to set procedure\n");
free_proc(procedure);
packet->data[0] = PROC_PUSH_RESPONSE;
packet->data[0] |= PROC_FLAG_END;
packet->data[0] |= PROC_FLAG_ERROR;
packet->length = 1;
csp_sendto_reply(packet, packet, CSP_O_SAME);
return;
}

packet->data[0] = PROC_PUSH_RESPONSE;
packet->data[0] |= PROC_FLAG_END;
Expand Down
56 changes: 0 additions & 56 deletions src/proc_store.c

This file was deleted.

Loading

0 comments on commit ef14513

Please sign in to comment.