public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* Closing events and releasing resources in notify function
@ 2017-03-24  6:22 Arka Sharma
  2017-03-24  6:37 ` Andrew Fish
  0 siblings, 1 reply; 4+ messages in thread
From: Arka Sharma @ 2017-03-24  6:22 UTC (permalink / raw)
  To: edk2-devel

Hi,

Is it ok to close the event and release the resource allocated to be
sent as context inside the notify function. May be something like this

VOID
EFIAPI
NotifyRoutine (
       EFI_EVENT Event,
       VOID  *Context
)
{
   //
   // Perform intended task
   //
   FreePool (Context);
   gBS->CloseEvent (Event);
}

In the above example Context was allocated before creating the event
and passed as a parameter to CreateEvent().

Regards,
Arka


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Closing events and releasing resources in notify function
  2017-03-24  6:22 Closing events and releasing resources in notify function Arka Sharma
@ 2017-03-24  6:37 ` Andrew Fish
  2017-03-24  7:07   ` Arka Sharma
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Fish @ 2017-03-24  6:37 UTC (permalink / raw)
  To: Arka Sharma; +Cc: edk2-devel


> On Mar 23, 2017, at 11:22 PM, Arka Sharma <arka.sw1988@gmail.com> wrote:
> 
> Hi,
> 
> Is it ok to close the event and release the resource allocated to be
> sent as context inside the notify function. May be something like this
> 
> VOID
> EFIAPI
> NotifyRoutine (
>       EFI_EVENT Event,
>       VOID  *Context
> )
> {
>   //
>   // Perform intended task
>   //
>   FreePool (Context);
>   gBS->CloseEvent (Event);

You should close the event before you free the context, or you will have a race. You need to make sure there is no way for EFI to signal the event before you free the context. 

This all assumes you don't have other code doing crazy stuff with the Context or Event. It would  be a different answer for CreateEventEx()

Thanks,

Andrew Fish

> }
> 
> In the above example Context was allocated before creating the event
> and passed as a parameter to CreateEvent().
> 
> Regards,
> Arka
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Closing events and releasing resources in notify function
  2017-03-24  6:37 ` Andrew Fish
@ 2017-03-24  7:07   ` Arka Sharma
  2017-03-24  7:08     ` Andrew Fish
  0 siblings, 1 reply; 4+ messages in thread
From: Arka Sharma @ 2017-03-24  7:07 UTC (permalink / raw)
  To: Andrew Fish; +Cc: edk2-devel

Thanks Andrew for your reply. In this case I am creating the event
with CreateEvent() and not with CreateEventEx(). I am attempting to
free the Context inside the notify function which will be called after
Event is signaled, and Context is only accessed before CreateEvent()
and inside the NotifyRoutine(). So the change you are suggesting is
inside the notify function I should call CloseEvent() first before
freeing Context right ?

On Fri, Mar 24, 2017 at 12:07 PM, Andrew Fish <afish@apple.com> wrote:
>
>> On Mar 23, 2017, at 11:22 PM, Arka Sharma <arka.sw1988@gmail.com> wrote:
>>
>> Hi,
>>
>> Is it ok to close the event and release the resource allocated to be
>> sent as context inside the notify function. May be something like this
>>
>> VOID
>> EFIAPI
>> NotifyRoutine (
>>       EFI_EVENT Event,
>>       VOID  *Context
>> )
>> {
>>   //
>>   // Perform intended task
>>   //
>>   FreePool (Context);
>>   gBS->CloseEvent (Event);
>
> You should close the event before you free the context, or you will have a race. You need to make sure there is no way for EFI to signal the event before you free the context.
>
> This all assumes you don't have other code doing crazy stuff with the Context or Event. It would  be a different answer for CreateEventEx()
>
> Thanks,
>
> Andrew Fish
>
>> }
>>
>> In the above example Context was allocated before creating the event
>> and passed as a parameter to CreateEvent().
>>
>> Regards,
>> Arka
>> _______________________________________________
>> edk2-devel mailing list
>> edk2-devel@lists.01.org
>> https://lists.01.org/mailman/listinfo/edk2-devel
>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Closing events and releasing resources in notify function
  2017-03-24  7:07   ` Arka Sharma
@ 2017-03-24  7:08     ` Andrew Fish
  0 siblings, 0 replies; 4+ messages in thread
From: Andrew Fish @ 2017-03-24  7:08 UTC (permalink / raw)
  To: Arka Sharma; +Cc: edk2-devel


> On Mar 24, 2017, at 12:07 AM, Arka Sharma <arka.sw1988@gmail.com> wrote:
> 
> Thanks Andrew for your reply. In this case I am creating the event
> with CreateEvent() and not with CreateEventEx(). I am attempting to
> free the Context inside the notify function which will be called after
> Event is signaled, and Context is only accessed before CreateEvent()
> and inside the NotifyRoutine(). So the change you are suggesting is
> inside the notify function I should call CloseEvent() first before
> freeing Context right ?
> 

Yes as your event can get signaled until you close it in the general case. 

Thanks,

Andrew Fish

> On Fri, Mar 24, 2017 at 12:07 PM, Andrew Fish <afish@apple.com> wrote:
>> 
>>> On Mar 23, 2017, at 11:22 PM, Arka Sharma <arka.sw1988@gmail.com> wrote:
>>> 
>>> Hi,
>>> 
>>> Is it ok to close the event and release the resource allocated to be
>>> sent as context inside the notify function. May be something like this
>>> 
>>> VOID
>>> EFIAPI
>>> NotifyRoutine (
>>>      EFI_EVENT Event,
>>>      VOID  *Context
>>> )
>>> {
>>>  //
>>>  // Perform intended task
>>>  //
>>>  FreePool (Context);
>>>  gBS->CloseEvent (Event);
>> 
>> You should close the event before you free the context, or you will have a race. You need to make sure there is no way for EFI to signal the event before you free the context.
>> 
>> This all assumes you don't have other code doing crazy stuff with the Context or Event. It would  be a different answer for CreateEventEx()
>> 
>> Thanks,
>> 
>> Andrew Fish
>> 
>>> }
>>> 
>>> In the above example Context was allocated before creating the event
>>> and passed as a parameter to CreateEvent().
>>> 
>>> Regards,
>>> Arka
>>> _______________________________________________
>>> edk2-devel mailing list
>>> edk2-devel@lists.01.org
>>> https://lists.01.org/mailman/listinfo/edk2-devel
>> 



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-03-24  7:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-24  6:22 Closing events and releasing resources in notify function Arka Sharma
2017-03-24  6:37 ` Andrew Fish
2017-03-24  7:07   ` Arka Sharma
2017-03-24  7:08     ` Andrew Fish

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox