Enforcing Correct Concurrent Access of Class Data Posted: 26 May 2011 05:50 PM PDT Hi, this is Jim Springfield. I'm an architect on the Visual C++ team. In any concurrent application, protecting data from concurrent access is extremely important. There are many primitives that can be used for this, such as critical sections, mutexes, reader-writer locks, etc. There are also some newer high-level approaches to concurrency such as those provided by the Concurrency Runtime, although this isn't the focus of what I'm showing here. However, there isn't a good way in C++ to make sure that you are really protecting data correctly when accessing it from multiple threads. You will often see a comment (likely made by the original author) next to a member that reminds you to take some lock when accessing the data. There may be many data items all using the same lock and there may be more than one lock, with some data protected by one lock and some by another. When it comes time to access some data from a member function, you have to start asking some questions. Who is going to call this member? What locks will already be held? Could I deadlock here? While I don't have a solution to all of these, I do have a technique that allows you to be more aggressive with trying things and more comfortable with making changes to existing code, while guaranteeing that you don't violate the requirement that a particular lock is held. What I'm going to show is a way to associate a lock with a data member such that whenever that data member is accessed, a check is made that the proper lock is held by the thread. The basis for the technique uses native properties to provide access to data members. With a small set of macros, you can easily retrofit existing code to provide this benefit. I developed this technique years ago and I have used it in several code bases to catch problems with concurrent access. Here is an example of something you will typically see in code. The developer has written that a critical section should be held when accessing m_rgContextsCache. - // Make sure m_cs is held when accessing m_rgContextsCache
- vector<FileConfig> m_rgContextsCache;
Wouldn't it be great if this information could be specified in code AND enforced? The code below shows how to transform this into just that. - PROTECTED_MEMBER(m_cs, vector<FileConfig>, m_rgContextsCache);
Now, whenever m_rgContextsCache is accessed, a user-defined function will be called if the proper lock is not held. What the macro does is to create the actual data member with a slightly modified name and a property with the name specified. Now, all you have to do is run your code and see if any errors occur. There is one "gotcha". When members are initialized in the constructor or referenced in a destructor, the lock isn't going to be held. For those cases, you need to directly access the member. A macro that translates a name into the modified "real" name can be used. It can also be used anywhere that it is specifically safe to access the member outside of the lock. The nice thing is that it is now very clear when you are doing this. Here is the code for this. - // The USN macro is used when you need to access a data member in an "unsafe" way.
- // This makes sense when you know no other thread is accessing it, such as in a constructor.
- #define USN(name) name##_usn_
The PROTECTED_MEMBER macro is defined below. The first line creates the actual member. The second line creates the property and the remaining lines implement the get and put. - #define PROTECTED_MEMBER(cs, type, name) \
- type USN(name);\
- __declspec(property(get=Get_##name, put=Put_##name)) type name;\
- type & Get_##name()\
- {_PROTECT(verify_lock(cs));return USN(name);}\
- type const & Get_##name() const\
- {_PROTECT(verify_lock(cs));return USN(name);}\
- type & Put_##name(type const & x)\
- {_PROTECT(verify_lock(cs));USN(name) = x;return USN(name);}
There are a couple of things that aren't defined yet. The verify_lock function will return a boolean indicating whether the lock is held or not. These can be defined for any type of lock you use. There is also the _PROTECT macro. This should be defined to do whatever you want in the case of a failure. This could log, assert, crash, etc. There are some other variations of the macro to handle some additional cases. One is to handle arrays. It provides a parameterized property which handles the index. - #define PROTECTED_MEMBER_ARRAY(cs, elemtype, name, length) \
- typedef elemtype type_##name[length];\
- elemtype USN(name)[length];\
- __declspec(property(get=Get_##name, put=Put_##name)) elemtype name[length];\
- elemtype& Get_##name(size_t i)\
- {_PROTECT(verify_lock(cs));return USN(name)[i];}\
- type_##name& Get_##name()\
- {_PROTECT(verify_lock(cs));return USN(name);}\
- const elemtype& Put_##name(size_t i, elemtype const& x)\
- {_PROTECT(verify_lock(cs));USN(name)[i] = x;return USN(name)[i];}
To handle a reader-writer lock, a slightly different macro is used. Instead of "verify_lock", two other functions are used: verify_readlock and verify_writelock. Again, these can be user-defined to handle any type of reader-writer lock. There is one additional wrinkle here, however. There is a function defined called "GetWritable_##name". The getter returns a const& to the underlying member and verifies that a read lock is held, but this won't allow you to call methods on it that modify it. To do that, you have to explicitly call GetWritable_##name. This will return a non-const reference and verify the write lock is held. - #define PROTECTED_MEMBER_RW(lock, type, name) \
- type USN(name);\
- __declspec(property(get=Get_##name, put=Put_##name)) type name;\
- const type & Get_##name()\
- {_PROTECT(verify_readlock(lock));return USN(name);}\
- type & Put_##name(type const& x)\
- {_PROTECT(verify_writelock(lock));USN(name) = x;return USN(name);}\
- __declspec(property(get=GetWritable_##name)) type Writable_##name;\
- type & GetWritable_##name()\
- {_PROTECT(verify_writelock(lock));return USN(name);}
There are a couple of other variations to the PROTECTED_MEMBER macro to handle some cases that can occur. If the data member can't be assigned to (i.e. it is a type without assignment), we need to not provide a "Put" or we will get a compile error. Similarly, we may have a type that can't be assigned from const data. These cases occur rarely in practice, but they do occur. - #define PROTECTED_MEMBER_NC(cs, type, name) \
- type USN(name);\
- __declspec(property(get=Get_##name, put=Put_##name)) type name;\
- type & Get_##name()\
- {_PROTECT(verify_lock(cs));return USN(name);}\
- type const & Get_##name() const\
- {_PROTECT(verify_lock(cs));return USN(name);}\
- template <typename T>\
- type & Put_##name(T x)\
- {_PROTECT(verify_lock(cs));USN(name) = x;return USN(name);}\
-
- #define PROTECTED_MEMBER_GET(cs, type, name) \
- type USN(name);\
- __declspec(property(get=Get_##name, put=Put_##name)) type name;\
- type & Get_##name()\
- {_PROTECT(verify_lock(cs));return USN(name);}\
- type const & Get_##name() const\
- {_PROTECT(verify_lock(cs));return USN(name);}
Finally, here are some examples of verify_lock and verify_unlock that can handle critical sections by pointer or by reference. - inline bool verify_lock(const CRITICAL_SECTION& cs)
- {
- return (cs.OwningThread == (HANDLE)(UINT_PTR)GetCurrentThreadId());
- }
- inline bool verify_unlock(const CRITICAL_SECTION& cs)
- {
- return (cs.OwningThread == (HANDLE)(UINT_PTR)0);
- }
-
- inline bool verify_lock(const CRITICAL_SECTION* cs)
- {
- return (cs->OwningThread == (HANDLE)(UINT_PTR)GetCurrentThreadId());
- }
- inline bool verify_unlock(const CRITICAL_SECTION* cs)
- {
- return (cs->OwningThread == (HANDLE)(UINT_PTR)0);
- }
What I typically do is put all of these macros in a header file under an #ifdef _PROTECT guard. If _PROTECT is not defined, then I simply let everything collapse to simple data members. For release builds, the code is just as fast as before. - #ifdef _PROTECT
- // All of the code from above
- #else
- #define USN(name) name
- #define PROTECTED_MEMBER(cs, type, name) type name;
- #define PROTECTED_MEMBER_NC(cs, type, name) type name;
- #define PROTECTED_MEMBER_GET(cs, type, name) type name;
- #define PROTECTED_MEMBER_RW(cs, type, name) type name;
- #define PROTECTED_MEMBER_ARRAY(cs, elemtype, name, length) elemtype name[length];
-
Silverlight TV 74: What's New in RIA Services Posted: 26 May 2011 09:34 AM PDT Silverlight TV 74: In this week's episode of SLTV, Deepesh Mohnani of the WCF RIA Services team discusses all of the new features introduced since the initial RIA Services release. These include expanded localization support, improved MVVM support, Windows...( read more )...( read more) | Creating Custom Ajax Control Toolkit Controls Posted: 26 May 2011 09:45 AM PDT The goal of this blog entry is to explain how you can extend the Ajax Control Toolkit with custom Ajax Control Toolkit controls. I describe how you can create the two halves of an Ajax Control Toolkit control: the server-side control extender and the client-side control behavior. Finally, I explain how you can use the new Ajax Control Toolkit control in a Web Forms page. At the end of this blog entry, there is a link to download a Visual Studio 2010 solution which contains the code for two Ajax Control Toolkit controls: SampleExtender and PopupHelpExtender. The SampleExtender contains the minimum skeleton for creating a new Ajax Control Toolkit control. You can use the SampleExtender as a starting point for your custom Ajax Control Toolkit controls...( read more) | WP7 for iPhone and Android Developers - Introducing the Execution Model Posted: 25 May 2011 02:00 PM PDT Don't miss... Free SilverlightShow Webinars WP7 series by Andrea Boschin Kevin's 'WP7 for iPhone Developers' book: Show more books This article is part 7 of a 12-part article series on Windows Phone 7 for iPhone and Android Developers . WP7 for iPhone...( read more )...( read more) | SilverlightShow for May 16-22, 2011 Posted: 25 May 2011 08:50 PM PDT Check out the Top Five most popular news at SilverlightShow for SilverlightShow Top 5 News for May 16-22, 2011. Here are the top 5 news on SilverlightShow for last week: More good times for app developers? Metro In Motion #5 – SandwichFlow Spying Silverlight...( read more )...( read more) | Bin Deploying ASP.NET MVC 3 Posted: 25 May 2011 08:51 PM PDT When you build an ASP.NET MVC 3 application and are ready to deploy it to your hosting provider, there are a set of assemblies you'll need to include with your application for it to run properly, unless they are already installed in the Global Assembly Cache (GAC) on the server. In previous versions of ASP.NET MVC, this set of assemblies was rather small. In fact, it was only one assembly, System.Web.Mvc.dll, though in the case of ASP.NET MVC 1.0, if you didn't have SP1 of .NET 3.5 installed, you would have also needed to deploy System.Web.Abstractions.dll and System.Web.Routing.dll . But ASP.NET MVC 3 makes use of technology shared with the new ASP.NET Web Pages product such as Razor. If you're not familiar with ASP.NET Web Pages and how it...( read more) | Globalization, Internationalization and Localization in ASP.NET MVC 3, JavaScript and jQuery - Part 1 Posted: 25 May 2011 07:56 PM PDT There are several books worth of information to be said about Internationalization (i18n) out there, so I can't solve it all in a blog post. Even 9 pages of blog posts . I like to call it Iñtërnâtiônàlizætiøn , actually. There's a couple of basic things to understand though, before you create a multilingual ASP.NET application. Let's agree on some basic definitions as these terms are often used interchangeably. Internationalization (i18n) - Making your application able to support a range of languages and locales Localization (L10n) - Making your application support a specific language/locale. Globalization - The combination of Internationalization and Localization Language - For example, Spanish generally. ISO code "es"...( read more) | New Addition to Windows Phone Quickstarts- Pivot and Panorama Controls Posted: 25 May 2011 11:51 AM PDT Windows Phone 7 includes Panorama and Pivot controls that you can use to display controls and data and navigate through them using their built-in swipe and pan gestures. To help you understand these controls we have added a new quickstart to our existing...( read more )...( read more) | Analysis Services Helper - SSMS Addin Posted: 24 May 2011 09:05 AM PDT The Analysis Services Helper is an add-in for SQL Server Management Studio that helps users with repetitive tasks involved with Analysis Services such as processing and deployment. It's developed in C# .Net 2.0, and tested with SSMS 2008 R2. |
Mango : What’s new in Windows Phone 7.1 Posted: 25 May 2011 11:34 AM PDT Just back from lovely Microsoft MVP Open Day 2011 at Hyderabad,Andhra Pradesh in India conducted at Microsoft India Development Center (Microsoft's next biggest center after Redmond and some amazing people work here !) We always see and talk about success...( read more )...( read more) | XNA for Silverlight developers: Part 11 - Tombstoning Posted: 24 May 2011 02:00 PM PDT Are you interested in creating games for Windows Phone 7 but don't have any XNA experience? Then sign up for a 3-day Online Training Course delivered by CompletIT and Peter Kuhn ! Check the course agenda Don't miss... What is Windows Phone series XNA...( read more )...( read more) | Silverlight Cream for May 24 2011 -- #1094 Posted: 24 May 2011 10:12 PM PDT Looks like bloggers figured out I'd post the submittals first, because today I got hammered with posts. In this 2nd in a row Submittal-only Issue: Alex Golesh ( -2- , -3- , -4- , -5- ), René Schulte , Alex van Beek , Kunal Chowdhury ( -2- ), Rudi Grobler...( read more )...( read more) | Silverlight 5 Window : The things those I did not like Posted: 24 May 2011 01:30 PM PDT In my previous post that I did yesterday, I discussed about the Multiple Windows Support in Silverlight 5 Beta and there I mentioned that though it is a wonderful feature but there are some things that I didn't like. Though it is still in beta, so it...( read more )...( read more) | Medical Friend ICD-10 EMR Posted: 11 May 2011 09:47 PM PDT Medical Friend is an Open Source version of an EMR/HIS system based on SQL Server 2008. This system is intended to improve Diabetic treatment. Medical Friend intends to be a Meaningful Use certified EMR eventually with: a Medical Repository DW, Claims Processing system, CMS file loader system, and finally an ICD-10 EMR Scheduling system. This project was created by Diabetics for Diabetics. | “Mango” from the trenches part 1: Adding a new live tile to the start page #wp7dev Posted: 24 May 2011 02:53 PM PDT At IdentityMine , we had the chance to develop a few applications featuring the new Windows Phone 7 features coming up in the update codenamed "Mango". This series will showcase a few of the new features included in Mango that we used in those apps. Part...( read more )...( read more) | Windows Phone Developer Tools 7.1 Beta now available! Posted: 24 May 2011 01:44 PM PDT The Windows Phone Developer Tools 7.1 Beta released today. Download them here . The tools enable you to get started developing applications for the next version of Windows Phone 7 Codenamed "Mango" and leverage new phone features while continuing to...( read more )...( read more) |
SQL Azure Migration Wizard v3.7 Posted: 31 Aug 2009 02:23 PM PDT SQL Azure Migration Wizard (SQLAzureMW) is designed to help you migrate your SQL Server 2005/2008 databases to SQL Azure. SQLAzureMW will analyze your source database for compatibility issues and allow you to fully or partially migrate your database schema and data to SQL Azure. | MVVM Light Toolkit patch for WP7 Mango Beta Posted: 24 May 2011 10:41 AM PDT On Tuesday the 24th of May 2011, the new version of the Windows Phone 7 SDK codenamed "Mango" was released to the public in beta. This is a huge iteration with an extremely large number of new APIs. Most exciting, the version of the Silverlight framework...( read more )...( read more) | Windows Phone Mango–What’s New? (“Push Notifications & Tiles” - Part 8 of 8) Posted: 24 May 2011 03:48 AM PDT Mango introduces some changes in Push Notifications mechanism which enables the developers to create more attractive scenarios. First feature I'll show in this post is a secondary tiles for application. Before Mango, every application could have only...( read more )...( read more) | Windows Phone Mango–What’s New? (“Silverlight/XNA Interoperability” - Part 7 of 8) Posted: 24 May 2011 03:46 AM PDT Windows Phone RTM didn't allowed to mix Silverlight and XNA content. Mango enables the scenarios where Silverlight content can be rendered along with XNA content. In the sample presented in this post we will build 2-pager Silverlight application with...( read more )...( read more) | Windows Phone Mango–What’s New? (“Fast Application Switch (FAS)” - Part 6 of 8) Posted: 24 May 2011 03:43 AM PDT The Windows Phone RTM operating system had only one active application and when application were sent to the background the state was serialized and kept in the application's isolated storage. This process called tombstoning and application in such state...( read more )...( read more) | Windows Phone Mango–What’s New? (“Background Agents” - Part 5 of 8) Posted: 24 May 2011 03:42 AM PDT One of most requested and discussed features of Mango release is "multitasking". In Mango the multitasking term has slightly different meaning than standard (PC) multitasking. Multitasking for Mango phones means ability to execute the code while application...( read more )...( read more) | Windows Phone Mango–What’s New? (“Camera” - Part 4 of 8) Posted: 24 May 2011 03:40 AM PDT All Windows Phone 7 devices equipped with camera. The minimum required camera resolution is 5 Mega pixels. For developers accession camera information enables many scenarios like image recognition, video chatting, augmented reality and others. From the...( read more )...( read more) | Windows Phone Mango–What’s New? (“New Sensors & Tooling Enhancements” - Part 3 of 8) Posted: 24 May 2011 03:38 AM PDT Windows Phone devices are modern devices. As such, they usually have few sensors such as built-in accelerometer, A-GPS, light sensor, magnetometer, etc. Windows Phone minimum hardware spec requires that all Windows Phone will have at least 4 of them ...( read more )...( read more) | Windows Phone Mango–What’s New? (“Local Data” - Part 2 of 8) Posted: 24 May 2011 03:37 AM PDT Mango provides API to use user's Contacts and Appointments. To search for appointments and contacts we need to use the Appointments and Contacts classes located under Microsoft.Phone.UserData namespace. In this post I'll show how to create the sample...( read more )...( read more) | Windows Phone Mango–What’s New? (“Local Database” - Part 1 of 8) Posted: 24 May 2011 03:34 AM PDT Mango release adds a local database engine to the device. The database engine on the phone is based on SQL CE engine. Mango applications use LINQ to SQL for all database operations. LINQ to SQL provides an object-oriented approach to working with data...( read more )...( read more) | Windows Phone Mango–What’s New? (“Overview” - Part 0 of 8) Posted: 24 May 2011 03:33 AM PDT The Beta version of "Windows Phone Mango" tools release and it is a time to see what is expected from developers point of view. This post is a first part of multi-part "What's new" series which focuses on new features of "Windows Phone Mango" (or shortly...( read more )...( read more) | Windows Phone “Mango” Developer Tools Beta Posted: 24 May 2011 03:31 AM PDT Windows Phone Developer Tools for Windows Phone "Mango" (7.1) Beta release announced and available for general download here: http://go.microsoft.com/?linkid=9772716 Documentation link: http://msdn.microsoft.com/en-us/library/hh237343(v=VS.96).aspx ...( read more )...( read more) | Working with the $(this) object in jQuery Posted: 24 May 2011 07:55 AM PDT I'm working on an HTML application. It's a Workout Timer and will become an HTML5 experiment that is "desktop deployable" and will store user profiles using HTML5 local storage, etc. Though the application is in the very early stages, I know where I'm going and I want everything to be highly configurable, and for those [...] Read More......( read more) | Podcast- Scott Hanselman Posted: 24 May 2011 05:45 AM PDT Scott Hanselman is a member of the ASP.NET team and discribes himself as the Program Manager of MISC. In this episode Scott and I discuss teh ASP.NET Team, different models for web development and evolving how we approach Web Based Problem Solving ! Resources Scott's Blog Listen Subscribe MP3 WMA M4A Zune iPod Download 3GP [...] Read More......( read more) | Integrating Twitter with your ASP.NET MVC 3 and Razor Web Application Posted: 23 May 2011 10:36 PM PDT Much has been talked about the new release of ASP.NET MVC, i.e. MVC 3 and the new Razor syntax and the ability to simplify the development. Razor uses the @ prefix for switching between code and HTML and that kind of simplifies it when compared to using <%: symbol as with the case in MVC and ASPX engines. Here is a nice article from David Ebbo on how all of these fit together Not just that, Razor comes with a lot of goodies. The Helper classes simplify in doing some of the common tasks, doing common scenarios like integrating Twitter, Facebook or PayPal into your applications. To begin with, am using the following 1. Visual Studio 2010 Service Pack 1. You can install it from Download details- Microsoft Visual Studio 2010 Service Pack 1 2...( read more) | Silverlight Cream for May 23, 2011 -- #1093 Posted: 23 May 2011 08:17 PM PDT In this all submittal Issue: Colin Eberhardt , Rudi Grobler , Michael Washington ( -2- , -3- , -4- ), Victor Gaudioso . Above the Fold: Silverlight: "New Silverlight Video Tutorial: How to create a Rotating Banner" Victor Gaudioso WP7: "A Simple Windows...( read more )...( read more) | Silverlight 5 Multiple Window Support Posted: 23 May 2011 01:30 PM PDT Multiple Window support is one of the important feature in Silverlight 5. Using this you can have additional top level Window on top of your trusted Silverlight Out-of-Browser application. Yes, you read correct. It only works in Out-of-Browser Silverlight...( read more )...( read more) | Free “Guathon” all day event in London on June 6th Posted: 23 May 2011 07:15 PM PDT The (awesome) UK developer community is holding another all day event with Steve Sanderson and me in London on June 6th. The event is free to attend, and the venue will be in Central London (at the ODEON Covent Garden). The website for the event is here . Content The event goes from 9am to 5pm, and will feature a bunch of great .NET content. The current agenda includes the following talks: Build an app using ASP.NET MVC 3, EF Code First, NuGet and IIS Express (ScottGu) We'll spend 2 hours building an application with some of the latest releases of the Microsoft Web Stack. You get to choose what app to build and then watch Scott code it on stage. See how the Microsoft web stack fits together, how to take advantage of great...( read more) | Jeremy Likness on Creating Custom Markup Extensions with Silverlight 5 Posted: 23 May 2011 01:39 PM PDT How are markup extensions useful? They provide a hook to perform functionality that would be impossible or overly complicated using traditional approaches such as attached properties or behaviors. One popular use of markup extensions is to provide access...( read more )...( read more) | Orchard list customization: first item template Posted: 23 May 2011 05:33 PM PDT I got this question more than once: "how can you use a different template for the first blog post?" The scenario is illustrated by this example: If you look at the default rendering for the list of posts in a blog, you'll see this: < ul class ="blog-posts content-items" > < li class ="first" > < article class ="content-item blog-post" > < header >... .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode...( read more) |
|