ReaderWriter Lock-free implementation - lock-free

I want to implement a simple multi-reader/single-writer lock using a volatile value.
Although _InterlockedXXX provides full fence barrier and, if i'm right, also "volatile" keyword, I want to know about possible defects and improvements. I have no experience with acquire/release semantics.
My code:
BOOL TryAcquireShared(LONG volatile *lpnValue)
{
LONG initVal;
do
{
initVal = *lpnValue;
if (initVal == 0x80000000L)
return FALSE; //a writer is active
}
while (_InterlockedCompareExchange(lpnValue, initVal+1, initVal) != initVal);
return TRUE;
}
VOID ReleaseShared(LONG volatile *lpnValue)
{
LONG initVal, newVal;
do
{
initVal = *lpnValue;
newVal = (initVal & 0x80000000L) | ((initVal & 0x7FFFFFFFL) - 1);
}
while (_InterlockedCompareExchange(lpnValue, newVal, initVal) != initVal);
return;
}
BOOL TryAcquireExclusive(LONG volatile *lpnValue)
{
LONG i, initVal, newVal;
do
{
initVal = *lpnValue;
if ((initVal & 0x80000000L) != 0)
return FALSE; //another writer is active or waiting
}
while (_InterlockedCompareExchange(lpnValue, initVal | 0x80000000L, initVal) != initVal);
//wait until no readers
while ((*lpnValue & 0x7FFFFFFFL) != 0)
::Sleep(1);
return TRUE;
}
VOID ReleaseExclusive(LONG volatile *lpnValue)
{
_InterlockedExchange(lpnValue, 0);
return;
}
Also, if you know a library that can handle this, please tell me.

TryAcquireShared should check if *lpnValue is 0x7FFFFFFFL before increment it.
ReleaseShared should assert that *lpnValue doesn't have the 0x80000000L bit set rather then trying to preserve it. An exclusive lock shouldn't exist if you are releasing a shared lock.
TryAcquireExclusive should only check if *lpnValue is zero before setting the 0x80000000L bit.
Both aquire shared and aquire exclusive should have a Sleep(1) call every a certain amount of spins.
I don't understand the following part in TryAcquireExclusive. Why would you wait for readers if you just got an exclusive lock?
while ((*lpnValue & 0x7FFFFFFFL) != 0)
::Sleep(1);

Related

Guessing Number Game Program not Functioning Correctly

in my program, the user sets a range of numbers for the computer to guess. The user then has to guess which number the computer chose with a limit of guesses starting at 5. There are several problems in my functioning program in which I do not understand how to fix. These errors include:
-The number of guesses left always remains at 0. It won't start at 5 and decrease by 1 each time I click the btnCheck button.
-Whenever I click the btnCheck button for a new guessing number, the statement if you've guessed too high or too low remains the same.
-When I press btnNewGame, the values I insert in my low value and my high value text inputs will not be cleared.
-How can the computer generate a random whole number based on what I set as the number range?
Revising my code down below will be much appreciated.
// This line makes the button, btnCheckGuess wait for a mouse click
// When the button is clicked, the checkGuess function is called
btnCheckGuess.addEventListener(MouseEvent.CLICK, checkGuess);
// This line makes the button, btnNewGame wait for a mouse click
// When the button is clicked, the newGame function is called
btnNewGame.addEventListener(MouseEvent.CLICK, newGame);
// Declare Global Variables
var computerGuess:String; // the computer's guess
var Statement:String; // Statement based on your outcome
// This is the checkGuess function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function checkGuess(e:MouseEvent):void
{
var LowValue:Number; // the user's low value
var HighValue:Number; // the user's high value
var UserGuess:Number; // the user's guess
var CorrectGuess:int; // the correct number
var FirstGuess:String; //the user's guess
// get the user's range and guess
LowValue = Number(txtinLow.text);
HighValue = Number(txtinHigh.text);
UserGuess = Number(txtinGuess.text);
// determine the number of the user
GuessesLeft = checkCorrectGuess(FirstGuess);
lblNumber.text = GuessesLeft.toString();
lblStatement.text = "You have guessed " + Statement.toString() + "\r";
}
// This is function checkColoursCorrect
// g1– the user's guess
function checkCorrectGuess(g1:String):int
{
var GuessesLeft:int = 5; // How many guesses are left
if (g1 != computerGuess)
{
GuessesLeft - 1;
}
else
{
GuessesLeft = 0;
}
return GuessesLeft;
}
// This is the newGame function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function newGame(e:MouseEvent):void
{
var Guess1:int; // computer's guess in numbers
var UserGuess1:int; // user's guess in numbers
Guess1 = randomWholeNumber(100,1); //It is not (100,1). How do I change this to the range the user put?
UserGuess1 = randomWholeNumber(100,1); //It is not (100,1). How do I change this to the range the user put?
if (Guess1 > UserGuess1) {
Statement = "TOO HIGH";
} else if (Guess1 < UserGuess1) {
Statement = "TOO LOW";
} else if (Guess1 == UserGuess1) {
Statement = "CORRECTLY";
}
txtinGuess.text = "";
lblStatement.text = "";
}
// This is function randomWholeNumber
// highNumber – the maximum value desired
// lowNumber – the minimum value desired
// returns – a random whole number from highNumber to lowNumber inclusive
function randomWholeNumber(highNumber:int,lowNumber:int):int //How do I make a whole random number based on the range the user made?
{
return Math.floor((highNumber - lowNumber + 1) * Math.random() + lowNumber);
}
To answer your questions...
You've declared GuessesLeft inside checkCorrectGuess() which means its a local variable that's being redefined every time you call the function. Futhermore, because you're passing in var FirstGuess:String; (an uninitialized, non-referenced string variable), (g1 != computerGuess) is returning false, and the answer is always 0.
GuessesLeft - 1; is not saving the result back to the variable. You need to use an assignment operator such as GuessesLeft = GuessesLeft - 1 or simply type GuessesLeft-- if all you want is to decrement. You could also write GuessesLeft -= 1 which subtracts the right from the left, and assigns the value to the variable on the left. See AS3 Operators...
You've already assigned values to these TextFields earlier; simply repeat the process inside of newGame() with a txtinLow.text = "" (same with high)
Use your variables. You defined them earlier in checkGuess() as UserGuess, LowValue, and HighValue
Be mindful that you only need to split out functionality into separate functions if that piece of code is likely to be called elsewhere. Otherwise, every function on the stack incurs more memory and performance hits. checkCorrectGuess() falls into that category and is therefore unnecessary.
Also, you are printing your feedback to the user in the newGame() function instead of checkGuess(). It seemed like an oversight.
btnCheckGuess.addEventListener(MouseEvent.CLICK, checkGuess);
btnNewGame.addEventListener(MouseEvent.CLICK, newGame);
// Global Variables
var computerGuess:int;
var remainingGuesses:int;
newGame();
function newGame(e:MouseEvent):void {
// Reset our guess limit
remainingGuesses = 5;
// Generate a new number
computerGuess = random(int(txtinLow.text), int(txtinHigh.text));
// Reset our readouts.
txtinGuess.text = "";
lblStatement.text = "";
}
function checkGuess(e:MouseEvent):void {
var guess:int = int(txtinGuess.text);
var msg:String;
if (guess == computerGuess) { // Win
remainingGuesses = 0; // Zero our count
msg = "CORRECT";
} else { // Missed
remainingGuesses--; // Decrement our count
if (guess > computerGuess) {
msg = "TOO HIGH";
} else if (guess < computerGuess) {
msg = "TOO LOW";
}
}
lblNumber.text = remainingGuesses.toString();
lblStatement.text = "You have guessed " + msg;
}
function random(low:int, high:int):int {
return Math.floor((high - low + 1) * Math.random() + low);
}

c linux - memset raise Segmentation Fault in Release build, printf somehow can 'fix' it?

I'm getting segmentation fault on CentOS 7 64-bit, Release build (there's no issue in debug build)
I tried adding printf to locate where exactly the segfault happens, for below code, segfault raises at: memset(len_p, 0, sizeof(*len_p));
Also, If I add printf to print something right before memeset, there's no segfault, as if the printf is doing some magic here.
Please help me to understand the issue here.
core dump uploaded
Thanks a lot!
code snippet (mysql prepared statement to query something):
(this function was called couple of times, it doesn't always raise segfault)
int mysql_odb_read(void **data_p, size_t *len_p, git_otype *type_p, git_odb_backend *_backend, const git_oid *oid)
{
MYSQL_BIND bind_buffers[2];
MYSQL_BIND result_buffers[3];
memset(bind_buffers, 0, sizeof(bind_buffers));
// bind the repo passed to the statement
bind_buffers[0].buffer = &(backend->mysql->repo);
bind_buffers[0].buffer_length = sizeof(backend->mysql->repo);
bind_buffers[0].length = &bind_buffers[0].buffer_length;
bind_buffers[0].buffer_type = MYSQL_TYPE_LONGLONG;
// bind the oid passed to the statement
bind_buffers[1].buffer = (void*)oid->id;
bind_buffers[1].buffer_length = 20;
bind_buffers[1].length = &bind_buffers[1].buffer_length;
bind_buffers[1].buffer_type = MYSQL_TYPE_BLOB;
if (mysql_stmt_bind_param(backend->mysql->odb_read, bind_buffers) != 0)
return GIT_ERROR;
// execute the statement
if (mysql_stmt_execute(backend->mysql->odb_read) != 0)
return GIT_ERROR;
if (mysql_stmt_store_result(backend->mysql->odb_read) != 0)
return GIT_ERROR;
if (mysql_stmt_num_rows(backend->mysql->odb_read) == 1) {
memset(result_buffers, 0, sizeof(result_buffers));
result_buffers[0].buffer_type = MYSQL_TYPE_TINY;
result_buffers[0].buffer = type_p;
result_buffers[0].buffer_length = sizeof(*type_p);
result_buffers[0].is_null = 0;
result_buffers[0].length = &result_buffers[0].buffer_length;
memset(type_p, 0, sizeof(*type_p));
result_buffers[1].buffer_type = MYSQL_TYPE_LONG;
result_buffers[1].buffer = len_p;
result_buffers[1].buffer_length = sizeof(*len_p);
result_buffers[1].is_null = 0;
result_buffers[1].length = &result_buffers[1].buffer_length;
printf("len_p:%lu\n",*len_p);//if i remove this printf call, there will be segfault
memset(len_p, 0, sizeof(*len_p));
result_buffers[2].buffer_type = MYSQL_TYPE_LONG_BLOB;
result_buffers[2].buffer = *data_p;
result_buffers[2].is_null = 0;
result_buffers[2].buffer_length = data_len;
result_buffers[2].length = &data_len;
//memset(&data_len, 0, sizeof(data_len));
if (mysql_stmt_bind_result(backend->mysql->odb_read, result_buffers) != 0)
return GIT_ERROR;
error = mysql_stmt_fetch(backend->mysql->odb_read);
if (data_len > 0) {
*data_p = malloc(data_len);
if(*data_p){
result_buffers[2].buffer_type = MYSQL_TYPE_LONG_BLOB;
result_buffers[2].buffer = *data_p;
result_buffers[2].is_null = 0;
result_buffers[2].buffer_length = data_len;
result_buffers[2].length = &data_len;
if (mysql_stmt_fetch_column(backend->mysql->odb_read, &result_buffers[2], 2, 0) != 0)
return GIT_ERROR;
}
else{
printf("odb_read, malloc returned NULL\n");
return GITERR_NOMEMORY;
}
}
error = GIT_OK;
} else
error = GIT_ENOTFOUND;
// free result
if (mysql_stmt_free_result(backend->mysql->odb_read) != 0)
return GIT_ERROR;
mysql_free_result(meta_result);
// reset the statement for further use
if (mysql_stmt_reset(backend->mysql->odb_read) != 0)
return GIT_ERROR;
return error;
}
It's difficult to tell the exact problem without dumps or other informations, but a possible explanation may be that the len_p is a dangling pointer.
This triggers the segfault, however, by calling printf, you are probably filling up the stack area where len_p is pointing, with printf data, hence remapping the pointer with a new value, avoiding the segmentation fault.

Auto manage and protect Created\Updated fields with Entity Framework 5

I want so every added\changed record will have a time stamp of creation\change.
But - so it will be easy to embed and easy to manage - automatically.
Overwrite the 'DbContext' class or embed this in the '.tt' file (Codefirst \ DBFirst)
The code assume so you have the fields 'CreatedOn'\'ModifiedOn' inside the POCO.
If you don't have them, or you have only one - the code will work fine.
Be aware! If you use a extension (as this one) so allow you to do batch updates or changes from a stored procedure - this will not work
EDIT:
I found the source of my inspiration - thanks 'Nick' here
public override int SaveChanges()
{
var context = ((IObjectContextAdapter)this).ObjectContext;
var currentTime = DateTime.Now;
var objectStateEntries = from v in context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified)
where v.IsRelationship == false && v.Entity != null
select v;
foreach (var entry in objectStateEntries)
{
var createdOnProp = entry.Entity.GetType().GetProperty("CreatedOn");
if (createdOnProp != null)
{
if (entry.State == EntityState.Added)
{
if (createdOnProp != null)
{
createdOnProp.SetValue(entry.Entity, currentTime);
}
}
else
{
Entry(entry.Entity).Property("CreatedOn").IsModified = false;
}
}
var modifiedOnProp = entry.Entity.GetType().GetProperty("ModifiedOn");
if (modifiedOnProp != null)
{
modifiedOnProp.SetValue(entry.Entity, currentTime);
}
}
return base.SaveChanges();
}

IWebBrowser2 IHTMLDocument2 CTRL+F dialog appears but finds no matches

I'm generating HTML pages from strings stored in a database by using the IHTMLDocument2 write(SAFEARRAY) method. This works OK.
When CTRL+F is pressed the Find dialog appears as expected, but there are never any matches. What is being searched by CTRL+F? Perhaps an object is missing(that I must create) that the search looks at?
Here's some relevant code:
CComPtr<IDispatch> m_spDisp;
CComPtr<IWebBrowser2> m_spWeb2;
HRESULT m_hr;
IHTMLDocument2* m_document;
BOOL CSwiftDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_BackMenuButton.SetToolTipText(_T("Back"));
m_bInitialised = true;
m_bBackClicked = false;
m_svURLList.clear();
m_nCurrentPage = -1;
m_bitBack.LoadBitmap(IDB_BACK_BITMAP);
m_BackMenuButton.SetBitmap(m_bitBack);
m_spGlobal.CreateInstance(__uuidof(GLOBVARSLib::Global ) );
m_browser.Navigate(CSTR m_sURL, NULL, NULL, NULL, NULL);
GetDocument();
WriteHTMLString();
SetWindowSize(512,384);
return TRUE;
}
void CSwiftDlg::GetDocument()
{
m_hr = S_OK;
m_spDisp = m_browser.get_Application();
if (m_spDisp != NULL && m_spWeb2 ==NULL)
{
m_hr = m_spDisp->QueryInterface(IID_IWebBrowser2,(void**)&m_spWeb2);
}
if (SUCCEEDED(m_hr) && m_spWeb2 != NULL)
{
// get browser document's dispatch interface
IDispatch *document_dispatch = NULL;
m_hr = m_spWeb2->get_Document(&document_dispatch);
if (SUCCEEDED(m_hr) && (document_dispatch != NULL))
{ // get the actual document interface
m_hr = document_dispatch->QueryInterface(IID_IHTMLDocument2, (void **)&m_document);
// release dispatch interface
document_dispatch->Release();
}
}
}
void CSwiftDlg::WriteHTMLString()
{
if (m_document == NULL)
GetDocument();
SAFEARRAY *empty_array = SafeArrayCreateVector(VT_VARIANT,0,1);
// construct text to be written to browser as SAFEARRAY
SAFEARRAY *safe_array = SafeArrayCreateVector(VT_VARIANT,0,1);
VARIANT *variant;
SafeArrayAccessData(safe_array,(LPVOID *)&variant);
variant->vt = VT_BSTR;
variant->bstrVal = m_sHTML.AllocSysString();
SafeArrayUnaccessData(safe_array);
// write SAFEARRAY to browser document
m_document->write(empty_array);
m_document->close();
m_document->write(safe_array);
}
Answer:
As #Yahia suggested, it was a focus problem. I added m_document->execCommand("Refresh",...) after the m_document->write(safe_array) statement, as when I did "refresh" from the context menu Ctrl-F worked as expected. That fixed the "focus issue".
CTRL+F is focus-aware... you need to call focus on the parentWindow of m_document after WriteHTMLString(); and/or SetWindowSize(512,384);...

Entity Framework SaveChanges function won't commit my changes

I have an initial selection which I place into list. I use the list to loop through each record and where it meets certain criteria I run trough a series of inserts, deletes and updates. Finally call the SaveChanges() method to commit changes.
The code runs through without raising an exception but no changes reflect in the database. I have been searching the web with no luck.
I'm using VS2008 with SQL2008 backend.
Please help?
using (SMSEntities db = new SMSEntities())
{
try
{
//Get SMS's to send from Inbox
List<Inbox> tmpInbox = (from c in db.Inboxes where c.Status != "NEW" && c.Status != "SUCCESS" select c).ToList();// new { Inbox.InboxID, Inbox.StatusTrackingID, Inbox.Status, Inbox.NoOfAttempts, Inbox.CellNo, Inbox.SourceCellNo, Inbox.Header, Inbox.Message, Inbox.MessageDate, Inbox.AccountID, Inbox.LastAttemptDate }).ToList();
foreach (Inbox tmpInboxIndex in tmpInbox)
{
bool success = false;
//Check status here
string SentStatus = CheckSMSSentToProvider(tmpInboxIndex.StatusTrackingID);
// Define a transaction scope for the operations.
using (TransactionScope transaction = new TransactionScope())
{
try
{
if ((SentStatus == "DELIVERED") || (SentStatus == "NOTFOUND") || (SentStatus == "DELETED") || (SentStatus == "REJECTED") || (SentStatus == "UNDELIVERED"))
{
//Insert the Log row
Log newLog = new Log();
newLog.InboxID = tmpInboxIndex.InboxID;
newLog.CellNo = tmpInboxIndex.CellNo;
newLog.SourceCellNo = tmpInboxIndex.SourceCellNo;
newLog.Message = tmpInboxIndex.Message;
newLog.Header = tmpInboxIndex.Header;
newLog.MessageDate = tmpInboxIndex.MessageDate;
newLog.AccountID = tmpInboxIndex.AccountID;
newLog.ProcessedDate = DateTime.Now;
newLog.Status = tmpInboxIndex.Status;
newLog.StatusTrackingID = tmpInboxIndex.StatusTrackingID;
newLog.NoOfAttempts = tmpInboxIndex.NoOfAttempts;
newLog.LastAttemptDate = tmpInboxIndex.LastAttemptDate;
db.Logs.AddObject(newLog);
//Delete the Inbox row
if (tmpInbox != null)
{
var deleteInbox = (from c in db.Inboxes where c.InboxID == tmpInboxIndex.InboxID select c).FirstOrDefault();
if (deleteInbox != null)
{
db.DeleteObject(deleteInbox);
//db.SaveChanges(SaveOptions.DetectChangesBeforeSave);
}
}
}
else
{
//Update inbox status
var tmpUpdateInbox = (from c in db.Inboxes where c.InboxID == tmpInboxIndex.InboxID select c).FirstOrDefault();
tmpUpdateInbox.Status = SentStatus;
tmpUpdateInbox.NoOfAttempts = tmpInboxIndex.NoOfAttempts + 1;
tmpUpdateInbox.LastAttemptDate = DateTime.Now;
//db.SaveChanges(SaveOptions.DetectChangesBeforeSave);
}
// Mark the transaction as complete.
transaction.Complete();
success = true;
//break;
}
catch (Exception ex)
{
// Handle errors and deadlocks here and retry if needed.
// Allow an UpdateException to pass through and
// retry, otherwise stop the execution.
if (ex.GetType() != typeof(UpdateException))
{
Console.WriteLine("An error occured. "
+ "The operation cannot be retried."
+ ex.Message);
break;
}
// If we get to this point, the operation will be retried.
}
}
if (success)
{
// Reset the context since the operation succeeded.
//db.AcceptAllChanges();
db.SaveChanges();
}
}
// Dispose the object context.
db.Dispose();
}
catch (Exception exp)
{
throw new Exception("ERROR - " + exp.Message.ToString(), exp);
}
}
return true;
Regards,
GPR.
Are you using a local database file? You may be looking for changes in the wrong place. By default, when the program starts, VS copies the database file into the debug or release folder. Then the program runs and changes are made, and saved, to the file in the debug or release folder. The program ends, and when you look at the database in your source folder it looks the same. You can change the connection string in the app.config to use an absolute path to avoid this.
See http://blogs.msdn.com/b/smartclientdata/archive/2005/08/26/456886.aspx for more info
The TransactionScope is useless if you do not put the call to SaveChanges into it.
Either move the call to SaveChanges into it or remove the TransactionScope completely.