编辑代码

#include <stdio.h>

typedef unsigned        char    uint8_t;
typedef unsigned short  int     uint16_t;
typedef unsigned        int     uint32_t; 
typedef void (*pFunc)(void);

#define NEWLINE   "\r\n"

struct systick_cb_block {
    pFunc cb;
    struct systick_cb_block *next;
};

struct systick_cb_block systick_callback;

void systick_callback_register(struct systick_cb_block* cb_blk) {
    struct systick_cb_block* p = &systick_callback;
    while (p->next) {
        if (p->next->cb == cb_blk->cb) {
            break;
        }
        p = p->next;
    }
    printf("0x%08X REG"NEWLINE, cb_blk);
    p->next = cb_blk;
}

void systick_callback_unregister(struct systick_cb_block* cb_blk) {
    struct systick_cb_block* p = &systick_callback;
    while (p->next) {
        if (p->next->cb == cb_blk->cb) {
            printf("0x%08X UNREG"NEWLINE, cb_blk);
            p->next->cb = NULL;
            p->next = p->next->next;
            break;
        }
        p = p->next;
    }
}

void systick_callback_dump(void) {
    struct systick_cb_block* p = &systick_callback;
    uint8_t index = 0;
    while (p->next) {
        if (p->next->cb) {
            printf("[%d]-[0x%08X]-[0x%08X]\n", index++, p->next, p->next->cb);
        } else {
            break;
        }
        p = p->next;
    }
}

void func1(void) {
    printf("11111"NEWLINE);
}
void func2(void) {
    printf("22222"NEWLINE);
}
void func3(void) {
    printf("33333"NEWLINE);
}

int main () {
    struct systick_cb_block *p;
    struct systick_cb_block k1,k2,k3;
    k1.cb = func1; k1.next = NULL;
    k2.cb = func2; k2.next = NULL;
    k3.cb = func3; k3.next = NULL;
    systick_callback_register(&k1);
    systick_callback_register(&k2);
    systick_callback_register(&k3);
    systick_callback_dump();
    systick_callback_unregister(&k2);
    systick_callback_dump();

    p = &systick_callback;
    while (p) {
        if (p->cb) 
            p->cb();
        p = p->next;
    }
    return 0;
}