platform for developing on SQFMI's Watchy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

63 lines
1.9 KiB

#ifndef _KERNEL_h
#define _KERNEL_h
#include "Task.h"
#define KERNEL_MAX_HANDLES 256
#define KERNEL_MAX_TASKS 32
typedef void (*kernel_handle_destructor_t)(kernel_handle_t, void*);
struct KernelHandle;
struct KernelTask;
class Kernel
{
private:
KernelHandle* m_handle[KERNEL_MAX_HANDLES];
int m_next_handle = 1;
KernelTask* m_task[KERNEL_MAX_TASKS];
void freeHandle(kernel_handle_t handle);
public:
Kernel();
/// <summary>
/// Create a new kernel handle pointing to an object
/// </summary>
/// <param name="type">Type of the handle, one of WATCHOS_HANDLE_TYPE_ consts</param>
/// <param name="object">Pointer to the object this handle references</param>
/// <param name="destructor">Function to call when this handle has released all references</param>
/// <param name="well_known">Specify a 'well_known' value this pointer can be fetched by</param>
/// <returns>A handle which can be used to fetch or release this object</returns>
kernel_handle_t allocHandle(byte type, void* object, kernel_handle_destructor_t destructor = nullptr, byte well_known = 0);
/// <summary>
/// Fetch the object referenced by this handle, increasing the reference count
/// </summary>
/// <param name="handle">Handle to fetch</param>
/// <returns>A pointer to the object the handle references</returns>
void* getHandle(kernel_handle_t handle);
/// <summary>
/// Fetch an object by its well-known value, increasing the reference count
/// </summary>
/// <param name="well_known">Well-known id of handle</param>
/// <returns>A pointer to the object the handle references</returns>
void* getHandle(byte well_known);
/// <summary>
/// Release a fetched reference to this object
/// </summary>
/// <param name="handle">Handle to release</param>
void releaseHandle(kernel_handle_t handle);
kernel_handle_t registerTask(Task* task);
void unregisterTask(Task* task);
void tick();
};
#endif