Friday, July 29, 2011

Reentrant lock acquires in CLR.

I always like when a post gets to the point (especially after searching in Google). In short, if you perform an operation that waits to acquire a lock, the thread doesn't actually stop.

In concrete terms of .NET coding, if you call the WaitOne method on a thread, it may still process events that the CLR deems "high priority". This can be a big problem if the event handlers go into the critical region you are trying to protect.

int criticalRegion = 0;

private void criticalRegionWait() 
{
   AutoResetEvent are = new AutoResetEvent(false);
   Thread thread = new Thread(() => 
      { 
         criticalRegion = -1
         Debug.WriteLine(criticalRegion);
         are.Set();
      });
   thread.SetApartmentState(ApartmentState.STA);
   thread.IsBackground = true;
   thread.Start();
}

private void criticalRegionUpdateEvent(object sender, EventArgs e) 
{
   criticalRegion = 1;
}
In the above example, if there is a high priority event that has criticalRegionUpdateEvent in its call path a race condition is possible. While criticalRegionWait waits for the thread to finish, the criticalRegionUpdateEvent has a chance of setting criticalRegion = 1 so that WriteLine ends up printing 1 instead of -1.

Friday, July 22, 2011

Namespaces for classes.

When designing classes for outside use (eg. API) or as a library component for multiple applications, readability and easy element (field/function) selection is important. No developer likes hunting around for fields or functions; a legible and hierarchical organization of elements aids quicker turnaround time in code delivery.

In some cases, a large class is needed that can possibly contain dozens or hundreds of elements. Naming convention can aid in this organization (ie. using common prefixes in field/function names) but are not inherent to the grammar of languages. Therefore, no semantic advantage can be taken from using these conventions.


class Store
   ItemList clothingMensShirts
   ItemList clothingMensShoes
   ...
   ItemList clothingWomensShirts
   ItemList clothingWomensShoes
   ...
   ItemList hardwareToolsWrenches
   ...
   ItemList hardwareMaterialsWood
   ...
   ItemList electronicsGamesPCFallout3Copies
   ...
   ItemList electronicsAudioHeadphones
   ...


The current solution to the management of large classes is by using publicly exposed nested classes.


class Clothing inherits Department
   ClothingSection mens
   ...

class Mens inherits ClothingSection
   ItemList shirt
   ItemList shoes
   ...

abstract class Store
   ...
   Department clothing
   ...

class IKEA inherits Store
   ...

IKEA newYorkStore = new IKEA();
newYorkStore.buy(newYorkStore.clothing.mens.shirts[0]);


This is a possible solution but also comes with it's own problems. Encapsulation of the nested class will not only hide information from the public user but also from the parent class as well. This can be problematic when designing an abstract class where the functions, that must be implemented, must also access non-public elements inside the nested. For example, in the store example, any data the top level class (Store) may want to access from the lower level ones (ClothingSection, ItemList) will be unavailable if that data must also be hidden from the public user.

Also, the nested classes cannot be abstracted to require implementation from any inheriting class. Revisting the store example, this means that abstracting the ClothingSection class would require two new classes for one implementation. Creating not only a child Store but also a child ClothingSection class.

Nested classes are helpful for creating logical hierarchies within a class or providing abstract functionality privately inside the class. Although, I would propose the use of namespacing inside a class instead. It would provide the logical hierarchy needed in a very simple way and allow for information sharing across the entire domain of the class. It would need a separate keyword though to prevent use outside of classes.


class Store

classnamespace clothing
   classnamespace mens
      ItemList shirts
      ItemList shoes
      ...
   ...
   classnamespace womens
      ItemList shirts
      ItemList shoes
      ...
   ...
   classnamespace hardware
      classnamespace tools
         ItemList wrenches
         ...
      ...
      classnamespace materials
         ItenList Wood
         ...
      ...
   ...

Tuesday, July 19, 2011

Delete long file path names.

I made the stupid mistake of importing an Eclipse project into my workspace when the import location was from the workspace itself. I went to delete the recursive folder when I encountered an error that the file path name was too long to delete.

The best manual solution that I found was to go as deep as possible in the recursive folder and then map it to a virtual drive.


subst x: c:\recursiveFolder\recursiveFolder\recursiveFolder\...


This can be quite an arduous process; I found myself creating up to 9 virtual drives. It eventually worked though and I was able to delete the recursive folder.


I also found a site with a Perl script that deletes long file paths but haven't tested it myself.

Wednesday, July 13, 2011

Representing scientific units in code.

I have been working on implementing a stream reader for IEEE 1451.4 TEDS and came across this interesting paper regarding representation of scientific units in code. It provides solutions to determining unit equivalency (eg. V/(m•kg/s²) = m/(s•A)). Also discovered some indivisible base units that can represent any unit (amp, candela, kelvin, gram, meter, mole, radian, second). With a bit to represent the placement (high for numerator, low for denominator) and a bit for presence, a unit storage type can be derived that only occupies 2 bytes.

Using Ubuntu 10.04 with SVN+HTTP.

I setup a server recently using Ubuntu 10.04. LAMP was easy to configure, as was Subversion. Although making Subversion and Apache2 play nice together with DAV SVN was a nightmare. I followed the guide provided but the website location just kept reprompting for my login.

After hours of running in circles, I was able to find the solution. The Require valid-user directive in /etc/apache2/mods-available/dav_svn.conf required the Limit directive to be around it.

<Limit GET POST PUT DELETE CONNECT OPTIONS PATCH PROPFIND PROPPATCH MKCOL COPY MOVE LOCK UNLOCK>
   Require valid-user
</Limit>
The LimitExcept directive can also be used as long as the limitations are specified.