0

I've got a problem with interrupt flag being reset. After setting the interrupt flag to 0 with asm cli, it comes to a line of code char* c = new char[size], and when it finishes initializing that array, it resets the I flag to 1. How can i make it that flag I stays on current value the whole time, because writing another asm cli after the new[] operator isn't the solution, giving that i have gap between those 2 instructions which enables the interrupts?

Thread::Thread(StackSize stackSize, Time timeSlice) {
#ifndef BCC_BLOCK_IGNORE
    DIS_INT // a macro: #define DIS_INT asm cli
#endif

    myPCB = new PCB(stackSize,timeSlice,this);

#ifndef BCC_BLOCK_IGNORE
    ENB_INT
#endif
}

this is where i set I flag to 0 and call the PCB constructor

PCB::PCB(StackSize stackSize, Time timeSlice, Thread* thread){
    time = timeSlice;
    myThread = thread;
    stack = createStack(stackSize);
    ...

char* PCB::createStack(StackSize stackSize){
    char* stek = new char[stackSize];         // after this line, IF = 1
#ifndef BCC_BLOCK_IGNORE                      // which is not desired
    newSS = FP_SEG(stek+stackSize);
    newSP = FP_OFF(stek+stackSize);
    asm{
        mov oldSS, ss
        mov oldSP, sp
        mov ss, newSS
        mov sp, newSP

        push ax
        push bx
        push cx
        push dx
        push es
        push ds
        push si
        push di
        push bp

        mov newSS, ss
        mov newSP, sp
        mov ss, oldSS
        mov sp, oldSP
    }
    this->stekp = MK_FP(newSS,newSP);
#endif
    return stek;
}
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
  • Use a critical protected section?? – πάντα ῥεῖ Aug 19 '15 at 19:11
  • You should probably share some details about OS and machine type, as well as what "interrupt flag" you're talking about. – ams Aug 19 '15 at 19:18
  • i use Windows 7 32bit, Intel proc, if thats what you're asking. I'm talking about the flag that disables/enables maskable interrupts. – Jovan Ristic Aug 19 '15 at 19:32
  • 1
    User mode programs can't disable interrupts under Windows. What do you think disabling interrupts would actually accomplish? – Ross Ridge Aug 19 '15 at 19:44
  • I don't want interrupts from keyboard for example to interfere in atomic actions like creating an object. As i said, CLI instruction works fine, it sets the I flag to 0, but it resets when it passes the new[] operator which is strange, i globally set I to 0, and i am asking a possible solution to this :/ – Jovan Ristic Aug 19 '15 at 19:47
  • Interrupts from the keyboard won't interfere with a user mode process creating an object. What you have to worry about is other threads (or maybe processes) that can potentially access the object interfering. Disabling interrupts, if it were possible, wouldn't prevent that if the other threads are running on a different CPU core. – Ross Ridge Aug 19 '15 at 19:51
  • Is this 16-bit Windows code? – Ross Ridge Aug 19 '15 at 19:56
  • yes. Ok, but what about timer() interrupt? Anyhow, as a demand in my project, that's explicitly said that i need to do it, i'm just wondering how to fix it, since the making of that array resets the IF. – Jovan Ristic Aug 19 '15 at 20:00
  • What if someone else, like heap allocation, tries to use the same trick? – Bo Persson Aug 19 '15 at 21:14
  • 1
    The interrupt flag is virtualized for 16-bit code run under 32-bit versions of Windows. It doesn't actually disable hardware interrupts. Since 16-bit tasks aren't preemptively multitasked and there are no threads you don't normally have to worry about atomicity. There's nothing else that can interfere. You only to clear the (virtual) interrupt flag in certain cases when working with (virtual) hardware. Your example doesn't need to disable interrupts. When you switch stacks the `mov ss, XXX` instruction automatically disables interrupts for the next instruction so it's done atomically. – Ross Ridge Aug 19 '15 at 21:16
  • Thanks for your answer. It's helpful. But would you mind, if you know, explaing to me why the main question of my problem here is happening? I am just interested in the reason why after explicitly saying to disable interrupts, it gets back on after creating that array? That's what intrigues me, whether i need it or not, you nicely explained it to me, but that doesnt resolve the nature of the problem i'm implying. – Jovan Ristic Aug 19 '15 at 23:40
  • 1
    I don't know why, but Bo Persson's suggestion is one possibility. I was going to say you don't have a problem so your question is moot, but looking at the code more closely you appear to have a bigger problem then you describe. You're apparently trying to implement your own pre-emptable threads. The C/C++ runtime you're using does not support this, as it's not reentrant. You'll need to disable interrupts or otherwise prevent thread switching during every call into the C/C++ runtime, including every use of operator new/delete. Some functionality, like exceptions, won't work at all. – Ross Ridge Aug 20 '15 at 02:25
  • Yes, that's exactly what i'm trying to do. I am disabling interrupts, but every call of new/delete operator resets the interrupt flag, and that's not how i want it to supposedly work (seeing how i globally disabled them), or i just don't know the mechanism behind those operators work, hence my question if i can somehow fix it, so that when it comes back from those operations, that interrupt flag still stays 0? I am not worried about exceptions because i don't need them. – Jovan Ristic Aug 20 '15 at 09:15
  • So you're writing a multi-threaded program that can only run as 16bit code under Windows with its virtualized interrupt flag. When running on raw hardware, you'll have a different stdlib that won't mess with the interrupt flag, or be in user mode where you can't touch it at all. Are you doing it while standing on your head, or with one arm tied behind your back, too? I mean seriously, it's really hard to imagine that this is the best choice for a new piece of software for any possible task in 2015. Can you give some background on why you're playing around with this? To see if it's possible? – Peter Cordes Aug 20 '15 at 18:44
  • Some people are truly ignorant. When I ask why something is manifesting the way it is and looking for answer for one particular thing, they will start shouting how what i am making is not how it should be made, or how stupid it is etc etc. I am not making a goddamn application for any kind of use in 2015 modern-information world. It's a study school project and i have certain limitations and certain instruction what i can and what i cannot use. – Jovan Ristic Aug 20 '15 at 23:43
  • Btw, while going through debugger, if i step over creating the PCB object, the IF stays 0, but if i step into it and go instruction by instruction, it'll be set to 1 when it comes to the new/delete operator – Jovan Ristic Aug 20 '15 at 23:44

0 Answers0