Hello,

I wrote a small class as wrapper for the rtl pthreads functions. All that works
fine only on remove module time the system is frozen. Maybee delete_pthread_np
is the wrong way ?? Attached the source.

Thanks
Olaf


/* -*- C++ -*- ***************************************************
 * $Id$
 *
 * Definition of ptRtTest class
 *
 * root <[EMAIL PROTECTED]>
 * Created on: <01/01/22 12:25:34 root>
 *
 */
extern "C" {
#include <linux/config.h>
#if defined(__KERNEL__) && defined(CONFIG_RTLINUX)
#include <rtlinux/rtl.h>
#include <rtlinux/posix/time.h>
#include <rtlinux/posix/pthread.h>
#include <rtlinux/rtl_printf.h>
#endif
}

extern "C" {
    void __pure_virtual() {}
}

class PosixThread {
    PosixThread(const PosixThread &);
    PosixThread& operator= (const PosixThread &);
    virtual void* threadProc() = 0;
public:
    enum cancel_state_t {
	enabled   = PTHREAD_CANCEL_ENABLE,
	disabled  = PTHREAD_CANCEL_DISABLE
    };
    enum cancel_type_t {
	deferred  = PTHREAD_CANCEL_DEFERRED,
	async     = PTHREAD_CANCEL_ASYNCHRONOUS
    };
public:
    PosixThread() {}
    virtual ~PosixThread() {}
    int start() { return pthread_create(&tid_, 0, PosixThread::execute, this); }
    static void setCancelState(cancel_state_t state, cancel_state_t* oldState = 0);
    static void setCancelType(cancel_type_t type, cancel_type_t* oldType = 0);
    static void* execute(void *thread);
    static int np_wait() { return pthread_wait_np(); }
    static int np_makePeriodic(hrtime_t start_time, hrtime_t period) {
	return pthread_make_periodic_np(pthread_self(), start_time, period); }
    static int np_delete() { return pthread_delete_np(pthread_self()); }
	
private:
    pthread_t tid_;
};

void PosixThread::setCancelState(cancel_state_t state, cancel_state_t* oldState = 0) { // static
    int old;
    pthread_setcancelstate(state, &old);
    if (oldState != 0)
	(*oldState) = (old == enabled) ? enabled : disabled;
}

void PosixThread::setCancelType(cancel_type_t type, cancel_type_t* oldType = 0) { // static
    int old;
    pthread_setcanceltype(type, &old);
    if (oldType != 0)
	(*oldType) = (old == deferred) ? deferred : async;
}

void* PosixThread::execute(void *thread) { // static
    void* retVal = 0;
    PosixThread* pPosixThread = static_cast<PosixThread*>(thread);

    setCancelState(disabled);
    setCancelType(deferred);
    retVal = pPosixThread->threadProc();

    return retVal;
}

class Tester : public PosixThread {
    Tester(const Tester &);
    Tester & operator = (const Tester &);
public:
    Tester() {}
    virtual ~Tester() { np_delete(); }
private:
    void* threadProc() {
	np_makePeriodic(gethrtime(), 500000000);
	while(1) {
	    np_wait();
	    rtl_printf("Hello World\n");
	}
    }
};


extern "C" {
    #include <linux/kernel.h>
    void *kmalloc(unsigned size, int prio);
    void kfree(void *p);
};

void *operator new (unsigned size) { return kmalloc(size,0); }
void *operator new[] (unsigned size, unsigned nb) { return kmalloc(size*nb,0); }
void operator delete (void *p) { kfree(p); }
void operator delete[] (void *p) { kfree(p);}


Tester* tester = 0;

extern "C" {
    int init_module(void);
    void cleanup_module(void);
}

int init_module(void) {
    rtl_printf("load module: ");
    tester = new Tester();
    tester->start();

    rtl_printf("done.\n");
    return 0;
}

void cleanup_module(void) {
    rtl_printf("unload module: ");
    delete tester;
    rtl_printf("done.\n");
}

Reply via email to