Skip to content

Commit

Permalink
Merge pull request #3 from ZhuLingQing/dev1
Browse files Browse the repository at this point in the history
add subfunction for test
  • Loading branch information
ZhuLingQing authored Mar 15, 2024
2 parents e13fb65 + 993e878 commit c69dacc
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 137 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ jobs:
- name: build with bash
run: bash bash_build.sh
- name: run tests
run: pushd build && ./test_pt_os &&popd
run: |
pushd build
./test_pt_os
popd
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
./build
build/
2 changes: 1 addition & 1 deletion bash_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ git clone https://github.com/ZhuLingQing/protothreads.git
g++ -g -o ${lib_name}.o -c ../pt-os.cpp -I./protothreads
ar -rv lib${lib_name}.a ${lib_name}.o
#build the test
g++ -g -o test_pt_os ../tests/*.cpp -I./protothreads -I.. -L. -l${lib_name}
g++ -g -o test_pt_os ../tests/*.cpp -I./protothreads -I.. -I../tests -L. -l${lib_name}
#run the test
./test_pt_os
rc=$?
Expand Down
110 changes: 61 additions & 49 deletions pt-os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

static inline size_t min_(size_t a, size_t b) { return a < b ? a : b; }

#define OsTaskId2Idx(id) (int)(id - kPt_)
#define OsTaskId2Idx(id) (int)(id - kPt_)

class TaskControlBlock
{
Expand All @@ -12,38 +12,45 @@ class TaskControlBlock

void Reset()
{
#if defined(osMaxNameLen) && osMaxNameLen > 0
memset(kName_, 0, sizeof(kName_));
#endif
#if defined(osMaxNameLen) && osMaxNameLen > 0
memset(kName_, 0, sizeof(kName_));
#endif
kTask_ = nullptr;
kPtStatus_ = OsTaskNotExist;
}

void Register(const char *name, TaskFunction task, void *param)
{
#if defined(osMaxNameLen) && osMaxNameLen > 0
memcpy(kName_, name, min_(sizeof(kName_) - 1, strlen(name)));
#endif
#if defined(osMaxNameLen) && osMaxNameLen > 0
memcpy(kName_, name, min_(sizeof(kName_) - 1, strlen(name)));
#endif
kTask_ = task;
kParam_ = param;
kEntered_ = false;
kPtStatus_ = OsTaskWaiting;
}
bool Yield(OsTaskId taskId)

bool Schedule(OsTaskId taskId)
{
OsTaskStatus newStatus;
if (kPtStatus_ == OsTaskExit) return false;
if (!kEntered_ && kPtStatus_ != OsTaskSuspend)
{
kEntered_ = true;
kPtStatus_ = (OsTaskStatus)kTask_(taskId, kParam_);
newStatus = (OsTaskStatus)kTask_(taskId, kParam_);
kEntered_ = false;
if (kPtStatus_ < OsTaskSuspend)
{
kPtStatus_ = newStatus;
return (kPtStatus_ == OsTaskExit) ? true : false;
}
return false;
}
return (kPtStatus_ == OsTaskExit)?true:false;
return (kPtStatus_ == OsTaskExit) ? true : false;
}

OsTaskStatus Status() { return kPtStatus_; }
int Suspend()
int Suspend()
{
if (kPtStatus_ == OsTaskNotExist) return INVALID_TASK_ID;
if (kPtStatus_ == OsTaskExit) return INVALID_TASK_STATUS;
Expand All @@ -58,7 +65,7 @@ class TaskControlBlock
return TASK_OP_SUCCESS;
}

int Delete()
int Delete()
{
if (kPtStatus_ == OsTaskNotExist) return INVALID_TASK_ID;
if (kPtStatus_ == OsTaskExit) return INVALID_TASK_STATUS;
Expand All @@ -68,18 +75,18 @@ class TaskControlBlock

const char *Name()
{
#if defined(osMaxNameLen) && osMaxNameLen > 0
return kName_;
#else
return nullptr;
#endif
#if defined(osMaxNameLen) && osMaxNameLen > 0
return kName_;
#else
return "N/A";
#endif
}

protected:
TaskFunction kTask_;
#if defined(osMaxNameLen) && osMaxNameLen > 0
#if defined(osMaxNameLen) && osMaxNameLen > 0
char kName_[osMaxNameLen];
#endif
#endif
void *kParam_;
bool kEntered_;
OsTaskStatus kPtStatus_;
Expand All @@ -100,22 +107,25 @@ class PtOs
return &kPt_[kIdx_ - 1];
}

void Yield()
void Schedule()
{
for (int i = 0; i < kIdx_; i++)
{
if (true == kTaskCb_[i].Yield(kPt_ + i)) kNumLiveTasks_--;
kCurrentTaskId_ = kPt_ + i;
if (true == kTaskCb_[i].Schedule(kPt_ + i)) kNumLiveTasks_--;
}
}

const char *Name(OsTaskId taskId)
{
if (taskId == OsSelfId) taskId = kCurrentTaskId_;
OS_ASSERT((taskId >= &kPt_[0] && taskId < &kPt_[osMaxThreads]));
return kTaskCb_[OsTaskId2Idx(taskId)].Name();
}

int Delete(OsTaskId taskId)
{
if (taskId == OsSelfId) taskId = kCurrentTaskId_;
OS_ASSERT((taskId >= &kPt_[0] && taskId < &kPt_[osMaxThreads]));
auto rc = kTaskCb_[OsTaskId2Idx(taskId)].Delete();
if (rc == TASK_OP_SUCCESS) kNumLiveTasks_--;
Expand All @@ -124,83 +134,85 @@ class PtOs

int Suspend(OsTaskId taskId)
{
if (taskId == OsSelfId) taskId = kCurrentTaskId_;
OS_ASSERT((taskId >= &kPt_[0] && taskId < &kPt_[osMaxThreads]));
return kTaskCb_[OsTaskId2Idx(taskId)].Suspend();
}

int Resume(OsTaskId taskId)
{
if (taskId == OsSelfId) taskId = kCurrentTaskId_;
OS_ASSERT((taskId >= &kPt_[0] && taskId < &kPt_[osMaxThreads]));
return kTaskCb_[OsTaskId2Idx(taskId)].Resume();
}

OsTaskStatus Status(OsTaskId taskId)
{
if (taskId == OsSelfId) taskId = kCurrentTaskId_;
OS_ASSERT((taskId >= &kPt_[0] && taskId < &kPt_[osMaxThreads]));
return kTaskCb_[OsTaskId2Idx(taskId)].Status();
}

int NumLiveTasks() { return kNumLiveTasks_; }
int NumOfLivingTasks() { return kNumLiveTasks_; }

void Reset()
{
for (int i = 0; i < osMaxThreads; i++)
kTaskCb_[i].Reset();
for (int i = 0; i < osMaxThreads; i++) kTaskCb_[i].Reset();
kIdx_ = 0;
kNumLiveTasks_ = 0;
}

OsTaskId CurrentTaskId() { return kCurrentTaskId_; }

protected:
TaskControlBlock kTaskCb_[osMaxThreads];
struct pt kPt_[osMaxThreads];
int kIdx_;
int kNumLiveTasks_;
OsTaskId kCurrentTaskId_;
};

static PtOs sOsControlBlock_;

OsTaskId RegisterTask(const char *name, TaskFunction task, void *param) { return sOsControlBlock_.RegisterTask(name, task, param); }

// Cause Registered Service get scheduled
void TaskYield(void)
OsTaskId RegisterTask(const char *name, TaskFunction task, void *param)
{
sOsControlBlock_.Yield();
return sOsControlBlock_.RegisterTask(name, task, param);
}

const char *TaskName(OsTaskId taskId)
{
return sOsControlBlock_.Name(taskId);
}
void TaskYield(void) { sOsControlBlock_.Schedule(); }

const char *TaskName(OsTaskId taskId) { return sOsControlBlock_.Name(taskId); }

int TaskDelete(OsTaskId taskId)
{
if (sOsControlBlock_.CurrentTaskId() == taskId) return INVALID_TASK_ID;
return sOsControlBlock_.Delete(taskId);
}

int TaskSuspend(OsTaskId taskId)
{
return sOsControlBlock_.Suspend(taskId);
int rc = sOsControlBlock_.Suspend(taskId);
if (rc != TASK_OP_SUCCESS) return rc;
if (sOsControlBlock_.CurrentTaskId() == taskId)
{
while (TaskStatus(taskId) == OsTaskSuspend) sOsControlBlock_.Schedule();
}
return TASK_OP_SUCCESS;
}

int TaskResume(OsTaskId taskId)
{
return sOsControlBlock_.Resume(taskId);
}
int TaskResume(OsTaskId taskId) { return sOsControlBlock_.Resume(taskId); }

OsTaskStatus TaskStatus(OsTaskId taskId)
{
return sOsControlBlock_.Status(taskId);
}
OsTaskStatus TaskStatus(OsTaskId taskId) { return sOsControlBlock_.Status(taskId); }

void OsInit(void)
int OsInit(void)
{
//make sure
OS_ASSERT(0 == sOsControlBlock_.NumLiveTasks());
// make sure all tasks are released
OS_ASSERT(0 == sOsControlBlock_.NumOfLivingTasks());
sOsControlBlock_.Reset();
return TASK_OP_SUCCESS;
}

void OsStart(void)
{
while (sOsControlBlock_.NumLiveTasks() > 0)
sOsControlBlock_.Yield();
while (sOsControlBlock_.NumOfLivingTasks() > 0) sOsControlBlock_.Schedule();
}
33 changes: 20 additions & 13 deletions pt-os.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
#ifndef _PT_OS_H_
#define _PT_OS_H_

#include "pt-osConfig.h"
#include <pt.h>
#include "pt-osConfig.h"

#define TASK_BEGIN(id) PT_BEGIN(id)
#define TASK_YIELD(id) PT_YIELD(id)
#define TASK_WAIT_UNTIL(id,cond) PT_WAIT_UNTIL(id, cond)
#define TASK_SUSPEND(id) do { if (taskId == id) PT_EXIT(id); else TaskSuspend(id); } while(0)
#define TASK_DELETE(id) do { if (taskId == id) PT_END(id); else TaskDelete(id); } while(0)
#define TASK_WAIT_UNTIL(id, cond) PT_WAIT_UNTIL(id, cond)
#define TASK_EXIT(id) return PT_ENDED
#define TASK_END(id) PT_END(id)
#define TASK_DECLARE(thread_declare) PT_THREAD(thread_declare)

#define TaskWaitUntil(cond) \
do \
{ \
while (!(cond)) TaskYield(); \
} while (0)

#if __cplusplus
extern "C"
{
#endif
typedef struct pt * OsTaskId;
#define OsInvlidTaskId ((OsTaskId)INVALID_TASK_ID)
typedef struct pt *OsTaskId;
#define OsInvlidTaskId ((OsTaskId)INVALID_TASK_ID)
#define OsSelfId ((OsTaskId)0)

#define TASK_OP_SUCCESS (0)
#define INVALID_TASK_ID (-1)
#define INVALID_TASK_STATUS (-2)
// return enumerations
#define TASK_OP_SUCCESS (0)
#define INVALID_TASK_ID (-1)
#define INVALID_TASK_STATUS (-2)

typedef enum
{
Expand All @@ -31,7 +37,7 @@ extern "C"
OsTaskSuspend = PT_EXITED,
OsTaskExit = PT_ENDED,
OsTaskNotExist = -1,
}OsTaskStatus;
} OsTaskStatus;

typedef char (*TaskFunction)(OsTaskId, void *);

Expand All @@ -42,18 +48,19 @@ extern "C"

const char *TaskName(OsTaskId taskId);

// Call TASK_DELETE, it will identify this task or not.
int TaskDelete(OsTaskId taskId);

// Call TASK_SUSPEND, it will identify this task or not.
int TaskSuspend(OsTaskId taskId);

// Resume the task from OsTaskSuspend.
int TaskResume(OsTaskId taskId);

// Get the OsTaskStatus.
OsTaskStatus TaskStatus(OsTaskId taskId);

void OsInit(void);
int OsInit(void);

// Start scheduling all the tasks, only return while all tasks are OsTaskExit or OsTaskNotExist.
void OsStart(void);

#if __cplusplus
Expand Down
5 changes: 4 additions & 1 deletion pt-osConfig.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#ifndef _OS_CONFIG_H_
#define _OS_CONFIG_H_


// Length of TaskName.
// may undef or set to zero for less ram resource.
#define osMaxNameLen (16)

// Number of maximum threads support
#define osMaxThreads (64)

#include <cassert>
Expand Down
14 changes: 5 additions & 9 deletions tests/1p1c.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#include "tz_ringbuf.hpp"
#include "pt-os.h"
#include <stdio.h>
#include <string.h>
#include "test.h"
#include "os_test.h"

static constexpr int kThreadNum = 1;

Expand Down Expand Up @@ -34,15 +30,15 @@ static inline int TestCheck()
DumpData(name, kConsData_[i]);
}

if (testFailed) printf("Test failed.\n");
if (testFailed) OS_TRACE("Test failed.\n");
return testFailed ? 1 : 0;
}

static TASK_DECLARE(prodTask(OsTaskId taskId, void *param))
{
static int i = 1;
TASK_BEGIN(taskId);
printf("%s Begin\n", TaskName(taskId));
OS_TRACE("%s Begin\n", TaskName(taskId));
while (1)
{
TASK_WAIT_UNTIL(taskId, !rb_.full());
Expand All @@ -59,7 +55,7 @@ static TASK_DECLARE(consTask(OsTaskId taskId, void *param))
static int i = 1;
int v;
TASK_BEGIN(taskId);
printf("%s Begin\n", TaskName(taskId));
OS_TRACE("%s Begin\n", TaskName(taskId));
while (1)
{
TASK_WAIT_UNTIL(taskId, !rb_.empty());
Expand All @@ -78,7 +74,7 @@ static TASK_DECLARE(consTask(OsTaskId taskId, void *param))

int Test1p1c()
{
printf("======== %s ========\n", __FUNCTION__);
OS_TRACE("======== %s ========\n", __FUNCTION__);
TestInit();

OsInit();
Expand Down
Loading

0 comments on commit c69dacc

Please sign in to comment.