Like Tree1Likes
  • 1 Post By brewbuck

threaded-safe exception handling?

This is a discussion on threaded-safe exception handling? within the C Programming forums, part of the General Programming Boards category; I'm thinking of implementing exception handling in C and extractly I have written a simple one. But it's not threaded-safe. ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    China
    Posts
    74

    Question threaded-safe exception handling?

    I'm thinking of implementing exception handling in C and extractly I have written a simple one. But it's not threaded-safe. Can you have me fix it, or is there any similar implementation for exception handling?
    Code:
    int __cm_jmp_pos = 0;
    jmp_buf __cm_jmp_buf[128];
    
    /* exception */
    /******************************************************************************/
    #define cm_try_begin() \
        do { \
            extern jmp_buf __cm_jmp_buf[128]; \
            extern int __cm_jmp_pos; \
            int __jmp_ret; \
            if((__jmp_ret = setjmp(__cm_jmp_buf[__cm_jmp_pos++])) == 0) \
            {
    
    #define cm_try_catch(exception) \
            } \
            else if(__jmp_ret == (exception)) \
            { \
                extern int __cm_jmp_pos; \
                __cm_jmp_pos--; \
    
    #define cm_try_end() \
            } \
            else \
            { \
                extern int __cm_jmp_pos; \
                __cm_jmp_pos--; \
                cm_try_throw(__jmp_ret); \
            } \
        } while(0)
    
    #define cm_try_throw(exception) \
        do { \
            extern jmp_buf __cm_jmp_buf[128]; \
            extern int __cm_jmp_pos; \
            if(__cm_jmp_pos > 0) \
            { \
                longjmp(__cm_jmp_buf[__cm_jmp_pos - 1], (exception)); \
            } \
            else \
            { \
                fprintf(stderr, "unhandled exception %d at file %s, line %d\n", \
                        (exception), \
                        __FILE__, \
                        __LINE__); \
                exit(EXIT_FAILURE); \
            } \
        } while(0)

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    Unrelated to your question, but you should not use names that have leading underscores. In most cases these are reserved for the implementation (in all cases two leading underscores are reserved). Your compiler, for example, is allowed to create something called __cm_jmp_buf for its own uses because it knows you're not supposed to. You can use another convention to mean "hands off", such as trailing underscores, or what have you.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you are on Windows platforms ... You should also investigate how their native SEH works...

    Structured Exception Handling (Windows)

    You will most likely discover that most of what you're doing is already implemented as a native part of the OS.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Location
    China
    Posts
    74
    Quote Originally Posted by CommonTater View Post
    If you are on Windows platforms ... You should also investigate how their native SEH works...

    Structured Exception Handling (Windows)

    You will most likely discover that most of what you're doing is already implemented as a native part of the OS.
    working under linux...

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    The simplest way to make what you already have thread-safe is to use a thread-specific array instead of a global array.

    Every time I've tried implementing or using such a system in C I've always hated it. Down this path is madness
    anduril462 likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    I think exceptions were one of the things that killed cfront, the original "pretranslator" that turned C++ code into something that was compileable by a C compiler. It's probably best that you learn from that failure.
    Down this path is madness
    QFT
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,165
    It is true that some problems led to the abandonment of "CFront", but the C++ exception handling mechanism is possible within standard C. The Comeau C/C++ produces such a bit of code.

    Such a beast will however require a lot more than what anyone could manage using nothing but the C standard preprocessor.

    And personally, I think using jump tables is starting at madness, who knows where you may end up?

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception safe constructor/destructor
    By Memloop in forum C++ Programming
    Replies: 24
    Last Post: 11-02-2009, 09:10 AM
  2. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  3. Exception-Safe Copy Assignment
    By George2 in forum C++ Programming
    Replies: 22
    Last Post: 04-02-2008, 05:43 AM
  4. using swap to make assignment operator exception safe
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2008, 05:32 AM
  5. Bjarne's exception safe sample
    By George2 in forum C++ Programming
    Replies: 13
    Last Post: 12-28-2007, 04:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21