Hi everyone,
I have looked at other posts that ask for similar subject but my requirement is relatively different and I am new to windows so I would like post a question and I hope that it is fine with you guys.
Basically what I want to do is to protect some kind of resources in my program using reference counting so that I do not release the resource if it is still being used.
In order to implement this, I assume that I need to keep tracking of reference counting and for this purpose I will want to increment/decrement a certain variable atomically.
My limitation is that I can only use function calls from NTDLL library.
Now I do not want to write an assembly code to implement atomic increment, atomic decrement, test and set and so on because I believe that there must be some functions that I can do this.
I searched the web and I can see that I might be able to do this with CriticalSection but I would like to ask experts here to get some insight/data to accomplish my job. I am sorry for very basic question but I am relatively new to windows development so it is painful for me to implement something I know the concept but not the practical working knowledge. If you have any pointers/web site, I will also appreciate it.
Thank you.
YEH
InterlockedIncrement()/InterlockedDecrement()
These happen to be implemented as intrinsic by the compiler.
-dave
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Monday, March 03, 2008 5:33 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] reference counting
Hi everyone,
I have looked at other posts that ask for similar subject but my requirement is relatively different and I am new to windows so I would like post a question and I hope that it is fine with you guys.
Basically what I want to do is to protect some kind of resources in my program using reference counting so that I do not release the resource if it is still being used.
In order to implement this, I assume that I need to keep tracking of reference counting and for this purpose I will want to increment/decrement a certain variable atomically.
My limitation is that I can only use function calls from NTDLL library.
Now I do not want to write an assembly code to implement atomic increment, atomic decrement, test and set and so on because I believe that there must be some functions that I can do this.
I searched the web and I can see that I might be able to do this with CriticalSection but I would like to ask experts here to get some insight/data to accomplish my job. I am sorry for very basic question but I am relatively new to windows development so it is painful for me to implement something I know the concept but not the practical working knowledge. If you have any pointers/web site, I will also appreciate it.
Thank you.
YEH
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
Thank you very much, David.
The only problem/limitation I have is that I cannot use kernel32 functions and that is why I am having trouble here. Could you advise any equivalent functions in NTDLL? Thank you very much.
YEH
The intrinsic versions of the functions begin with “_.” For example,
_InterlockedDecrement. However, in order to use an intrinsic, you
either must compile with -Oi, or explicitly force generation of an
intrinsic using #pragma intrinsic (function). If you’re using BUILD,
you can add “-Oi” to your USER_C_FLAGS, though there may be a better way
to do this. Also, as it is possible to explicitly force an actual
function call using #pragma function, if you enable intrinsics and get
unresolved linker errors, you may need to add #pragma
intrinsic(_InterlockedIncrement) to a translation unit to prevent linker
errors, should some header file or compiler switch elsewhere be
disabling their use. Personally, I would add the explicit #pragmas.
Finally, I’m not really sure how other compiler switches, like
Ox/Od/Os/Ot/O1/O2, for example, interact with intrinsics.
Good luck,
mm
xxxxx@gmail.com wrote:
Thank you very much, David.
The only problem/limitation I have is that I cannot use kernel32 functions and that is why I am having trouble here. Could you advise any equivalent functions in NTDLL? Thank you very much.
YEH
Yeh,
Do a little searching. You surely are including *some* header files from either the DDK or SDK to define the basic environment in which your code is to run. Both the DDK and SDK define interlocked routines for increment an decrement of integer sized operations. Even the compiler headers define them (as intrinsic). This is very basic stuff. Look at some samples. You don’t need equivalent functions from NTDLL. This is a fundamental compiler feature.
-dave
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Monday, March 03, 2008 10:07 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] reference counting
Thank you very much, David.
The only problem/limitation I have is that I cannot use kernel32 functions and that is why I am having trouble here. Could you advise any equivalent functions in NTDLL? Thank you very much.
YEH
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
InterlockedIn/Decrement are IIRC compiler intrinsic functions
–
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com
wrote in message news:xxxxx@ntdev…
> Hi everyone,
>
> I have looked at other posts that ask for similar subject but my requirement
is relatively different and I am new to windows so I would like post a question
and I hope that it is fine with you guys.
>
> Basically what I want to do is to protect some kind of resources in my
program using reference counting so that I do not release the resource if it is
still being used.
> In order to implement this, I assume that I need to keep tracking of
reference counting and for this purpose I will want to increment/decrement a
certain variable atomically.
>
> My limitation is that I can only use function calls from NTDLL library.
>
> Now I do not want to write an assembly code to implement atomic increment,
atomic decrement, test and set and so on because I believe that there must be
some functions that I can do this.
>
> I searched the web and I can see that I might be able to do this with
CriticalSection but I would like to ask experts here to get some insight/data
to accomplish my job. I am sorry for very basic question but I am relatively
new to windows development so it is painful for me to implement something I
know the concept but not the practical working knowledge. If you have any
pointers/web site, I will also appreciate it.
> Thank you.
>
> YEH
>
>
> My limitation is that I can only use function calls from NTDLL library.
Just a question - what are you trying to do??? Unless you are writing some application that does not run under Win32 subsystem (native application, POSIX one,etc) you should be able to call kernel32 exports If you are writing a driver… well, then you cannot call ntdll.dll anyway, because privileged code is not allowed to call unprivileged one. Therefore, in such case you have to call KeInterlockedXXX functions.
Anton Bassov