Author Topic: Handle exceptions inside a critical section  (Read 197 times)

2B||!2B

  • Member
  • **
  • Posts: 72
Handle exceptions inside a critical section
« on: December 02, 2019, 06:37:38 PM »
Is there an easy way to handle an exception when a lock is acquired?

For example, inside a critical section, there may be an exception generated. How to leave critical section in such case?

AW

  • Member
  • *****
  • Posts: 2442
  • Let's Make ASM Great Again!
Re: Handle exceptions inside a critical section
« Reply #1 on: December 03, 2019, 03:13:20 AM »
AFAIK, there are no special ASM facilities to handle them any easier than a HLL.
And real ASM programmers don't care about exceptions - good ASM code never faults.  :cool:

2B||!2B

  • Member
  • **
  • Posts: 72
Re: Handle exceptions inside a critical section
« Reply #2 on: December 03, 2019, 07:14:33 AM »
Good point AW. However, it is nice addition to have just in case because if such things happened, a deadlock is very likely to happen.
I have seen a concept called RAII in OOP languages where the object constructor encapsulates EnterCriticalSection and the destructor calls LeaveCriticalSection. Stack unwind does it all in case an exception is generated.

Code: [Select]
struct CSHolder {
    explicit CSHolder(CRITICAL_SECTION& cs): lock(cs) {
        ::EnterCriticalSection(&lock);
    }
    ~CSHolder() { ::LeaveCriticalSection(&lock); }
    CRITICAL_SECTION& lock;
};


CRITICAL_SECTION gLock;
void foo() {
    CSHolder lockIt(gLock);
    // lock is held until lockIt is destroyed
}

It could also be done in assembly with masmbasic try/catch macro i think.

caballero

  • Member
  • *****
  • Posts: 1214
  • Matrix - Noah
    • abre ojos ensamblador
Re: Handle exceptions inside a critical section
« Reply #3 on: December 03, 2019, 07:47:37 PM »
There's no magic in error handlings, it is just predictive codes.
The logic of the error is hidden among the most unexpected lines of the program

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6768
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Handle exceptions inside a critical section
« Reply #4 on: December 04, 2019, 08:59:16 AM »
AW is correct here, the most reliable code is code with NO ERRORS. The only place where error handling is truly needed is in dealing with external hardware factors that cannot be predicted. Write error free code and you save yourself a pile of crap overhead, trying to emulate the blunders of high level code is a waste of life and time, its main purpose is to protect people who do not have the skills to write error free code while learning.

In terms of debugging code with errors, something that crashes immediately can be directly debugged without having to untangle a mountain of crap to get there.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 9802
  • Assembler is fun ;-)
    • MasmBasic
Just-in-time debugging
« Reply #5 on: December 04, 2019, 02:48:33 PM »
In terms of debugging code with errors, something that crashes immediately can be directly debugged

More precisely, you can catch crashes by using just-in-time debugging. With Olly 2, for example, you can set it in Options/Options/Debugging/Just-in-time. If your code throws an exception, either unintentional or by hitting an INT 3, a box pops up and you can choose between closing or debugging the program.

mikeburr

  • Member
  • **
  • Posts: 96
Re: Handle exceptions inside a critical section
« Reply #6 on: December 12, 2019, 05:54:42 AM »
if youre using files then a much safer method of handling files is by putting a Soft Lock on the data. Basically what i mean is to put a data element in the record ... read the record and if the lock [byte] is not set then get the record set it and replace it . Any other potential user then knows the record is in use for update though reading of the current dta might..[????] be ok .[This is difficult in a multi threading environment btw .]. the same principle can be applied with elements of memory etc .. The real problem then is dealing with 'deadly embrace' however if you include a notion of the user of the locked record as well on the data item [eg a short user code] then you have the potential to notify them that you need their record etc etc The reason for this is that in a commercial environment things like users go to lunch leaving their work and stuff locked up , probably the most common thing other than say a file down for backup/overhaul [eg reindexing]  ..Im not sure if this is of any help to your problem but you are effectively managing the problem yourself rather than the operating system generalised API's ... as for multi threading instructions like XCHG will lock the bus and can be used to halt processes but this is very crude and fraught with danger when compared to the sophistication of the OS .. unux is far better in this respect
regards mike b