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.
 
 

68 lines
2.2 KiB

#ifndef _KERNEL_h
#define _KERNEL_h
#include "Task.h"
#include "EventListener.h"
#define KERNEL_MAX_HANDLES 256
#define KERNEL_MAX_TASKS 32
#define KERNEL_MAX_EVENT_SUBSCRIPTIONS 64
typedef void (*kernel_handle_destructor_t)(kernel_handle_t, void*);
struct KernelHandle;
struct KernelTask;
struct KernelEventSubscription;
class Kernel
{
private:
KernelHandle* m_handle[KERNEL_MAX_HANDLES];
int m_next_handle = 1;
KernelTask* m_task[KERNEL_MAX_TASKS];
KernelEventSubscription* m_event_subscription[KERNEL_MAX_EVENT_SUBSCRIPTIONS];
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, uint32_t 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);
void useHandle(kernel_handle_t handle);
/// <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 getHandleForWellKnown(well_known_handle_t handle);
kernel_handle_t registerTask(Task* task);
void unregisterTask(Task* task);
void pushEvent(well_known_handle_t source, uint16_t event, byte param1, byte param2);
kernel_handle_t subscribeEvent(EventListener* listener, well_known_handle_t source_mask, uint16_t event_mask);
void unsubscribeEvent(KernelEventSubscription* subscription);
void tick();
};
#endif